Skip to content

Feature/copy log as curl (#1458)#1683

Merged
255kb merged 15 commits intomockoon:feature/copy-as-curlfrom
Mavbraz:feature/copy-log-as-curl
Nov 10, 2025
Merged

Feature/copy log as curl (#1458)#1683
255kb merged 15 commits intomockoon:feature/copy-as-curlfrom
Mavbraz:feature/copy-log-as-curl

Conversation

@Mavbraz
Copy link
Contributor

@Mavbraz Mavbraz commented Feb 24, 2025

Technical implementation details

Copy the log as cURL with the same headers, body and other properties was sent in the request.

Checklist

  • data migration added (@mockoon/commons)
  • commons lib tests added (@mockoon/commons)
  • commons-server lib tests added (@mockoon/commons-server)
  • CLI tests added (@mockoon/cli)
  • desktop UI automated tests added (@mockoon/desktop)

Closes #1458

@Mavbraz
Copy link
Contributor Author

Mavbraz commented Feb 24, 2025

FYI: It's still needed to create some test coverage related to other methods and probably fix the existing ones (unfortunately I'm having a bad time testing on Manjaro). With that, said there's still work to be done.

@Mavbraz
Copy link
Contributor Author

Mavbraz commented Feb 24, 2025

Hello, @255kb! I was thinking to add a new context menu when it's a proxied request to the user be able to copy either the Mockoon request or the proxied request made by Mockoon. What are your thoughts on that?


I'd also like to hear your concerns about changing the http utils with the default value as {}. In my perspective I could remove this default {} to have more reliability of unit tests, assuring the body is sent only when requested. Context: https://github.com/mockoon/mockoon/blob/main/packages/desktop/test/libs/http.ts#L102
I found this behavior when I was creating the unit tests for this PR. If you do want to keep it as it is, I'll try to workaround to make sure some tests cover when the body is not sent.

@255kb
Copy link
Member

255kb commented Mar 5, 2025

Hello, @255kb! I was thinking to add a new context menu when it's a proxied request to the user be able to copy either the Mockoon request or the proxied request made by Mockoon. What are your thoughts on that?

Could you provide an example of the difference between the two? I guess it would be the URL? My first reaction: is it really useful to copy a CURL command that is not pointing to the Mockoon instance?

I'd also like to hear your concerns about changing the http utils with the default value as {}. In my perspective I could remove this default {} to have more reliability of unit tests, assuring the body is sent only when requested. Context: https://github.com/mockoon/mockoon/blob/main/packages/desktop/test/libs/http.ts#L102 I found this behavior when I was creating the unit tests for this PR. If you do want to keep it as it is, I'll try to workaround to make sure some tests cover when the body is not sent.

Feel free to improve this util. It is quite old! Just make sure that it's still working for all the other tests (obviously ;) ).

@regalstreak
Copy link

@Mavbraz tried it, works well. One suggestion:

We should add --compressed as default when we copy for convenience if there's a header --header 'accept-encoding: gzip, deflate' in the request that's copied. We could make a setting option for this?

@Mavbraz Mavbraz force-pushed the feature/copy-log-as-curl branch from 26047ea to 4900229 Compare May 21, 2025 23:05
@Mavbraz
Copy link
Contributor Author

Mavbraz commented May 22, 2025

@regalstreak that's a good suggestion, but there're many compression types not mentioned. e.g: br and zstd.
IMO we should add the --compression if we have the accept-encoding header not blank.

I agree it should have a setting to configure its use and IMO it should be true by default.

What are your thoughts on that matter?

@255kb
Copy link
Member

255kb commented Jun 4, 2025

@regalstreak @Mavbraz Do we really need a setting for that? My understanding was that curl's --compressed flag would work or not but only fail if the server use an unsupported compression method.
I would say we could always include the flag. But I'm also not a curl specialist :)

@Mavbraz PR seems kind of ready. Do you think we can finalize it in the coming week or two to include it in v9.3.0?

@255kb
Copy link
Member

255kb commented Oct 15, 2025

@Mavbraz Do you think you could finalize by adding the --compress flag? Then we could move on with the review and hopefully finalize for v9.4.0 due soon.

@Mavbraz
Copy link
Contributor Author

Mavbraz commented Oct 18, 2025

@255kb, I think this could be a good setting, at least in my case. Sometimes I need to copy the request and the allowed compression algorithms exactly.
That said, I’ll enable this compression flag by default.

@Mavbraz
Copy link
Contributor Author

Mavbraz commented Oct 18, 2025

I'll rebase this branch and push all my changes soon. Probably, it'll be possible to move with the review soon and make it to next release.

@Mavbraz Mavbraz force-pushed the feature/copy-log-as-curl branch from 397869f to 2399b1b Compare October 18, 2025 16:24
@255kb
Copy link
Member

255kb commented Oct 18, 2025

@255kb, I think this could be a good setting, at least in my case. Sometimes I need to copy the request and the allowed compression algorithms exactly. That said, I’ll enable this compression flag by default.

Thank you for updating the PR!

I still don't see why we would need a setting. The curl command should replicate the request, if it had an accept encoding header it should include the compression flag, if not it shouldn't. Isn't it too complicated to change the setting instead of just removing the compression flag from the copied command if needed?

@Mavbraz Mavbraz force-pushed the feature/copy-log-as-curl branch from a68a675 to 832f68f Compare October 24, 2025 04:31
@Mavbraz
Copy link
Contributor Author

Mavbraz commented Oct 24, 2025

It was rethinking about this setting and indeed it was not much valuable as I though.
Probably, I wouldn't use that much and if I need some day I'll just change it manually.

@255kb 255kb requested a review from Copilot October 24, 2025 09:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds the ability to copy environment logs as cURL commands, allowing users to recreate HTTP requests from the logs directly in their terminal or command line interface.

  • Adds a "Copy as cURL" action to the environment logs dropdown menu
  • Implements cURL command generation with proper handling of headers, request body, and compression settings
  • Adds comprehensive test coverage for various scenarios including compression handling and different request types

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/app/test/specs/environment-logs.spec.ts Adds multiple test cases validating cURL command generation for different scenarios (with/without compression, various headers, query parameters)
packages/app/test/libs/utils.ts Adds COPY_AS_CURL enum value to DropdownMenuLogsActions
packages/app/test/libs/http.ts Refactors body handling to support empty string bodies in HTTP calls
packages/app/test/libs/environments-logs.ts Adds helper method to click the "Copy as cURL" button in tests
packages/app/src/renderer/app/services/environments.service.ts Implements core cURL command generation logic with compression detection and header filtering
packages/app/src/renderer/app/services/data.service.ts Adds bodyUnformatted field to preserve original request body
packages/app/src/renderer/app/models/environment-logs.model.ts Adds bodyUnformatted property to EnvironmentLogRequest model
packages/app/src/renderer/app/components/environment-logs/environment-logs.component.ts Adds "Copy as cURL" menu item to the logs dropdown

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@255kb
Copy link
Member

255kb commented Oct 24, 2025

Thanks for the update!
Is it ready for review? I plan to do the release early next week.

Btw, I ran the CI and some tests are not passing.

@Mavbraz Mavbraz force-pushed the feature/copy-log-as-curl branch from 7c5d4a1 to 687c63b Compare October 25, 2025 20:51
@Mavbraz
Copy link
Contributor Author

Mavbraz commented Oct 25, 2025

This latest commit I updated some models to get the original headers sent, I also fixed the failing tests and refactored the command logic to use a builder as it would give much better readability and maintainability.

That said, I think now it's good to go with the reviewing, but there are just a few points I would like to discuss first:

  1. I used the --data-binary since it reproduce the same amount of bytes as the user sent and that's useful if you also use Mockoon as proxy as I do. With that, it's easier to reproduce some bug or something else. What are your thoughts on that? I'd like to keep that to help out many people who uses Mockoon as a Mock and Proxy at the same time
  2. The command may generate breaking lines due to the body content as a result of the point I said above, but I do not generate any command breaking line for terminals since it differ on OSes (Mac/Linux and Windows) and terminals (Terminal, powershell, cmd, etc). I couldn't find a good solution to work with all that and it'd give many maintainability problems in the future. Then it'd the users responsability of doing that. Very similar as the default postman behavior. Do you agree or have any better idea?

@255kb 255kb marked this pull request as ready for review October 27, 2025 13:31
@255kb 255kb self-requested a review as a code owner October 27, 2025 13:31
@255kb
Copy link
Member

255kb commented Oct 27, 2025

This latest commit I updated some models to get the original headers sent, I also fixed the failing tests and refactored the command logic to use a builder as it would give much better readability and maintainability.

That said, I think now it's good to go with the reviewing, but there are just a few points I would like to discuss first:

  1. I used the --data-binary since it reproduce the same amount of bytes as the user sent and that's useful if you also use Mockoon as proxy as I do. With that, it's easier to reproduce some bug or something else. What are your thoughts on that? I'd like to keep that to help out many people who uses Mockoon as a Mock and Proxy at the same time
  2. The command may generate breaking lines due to the body content as a result of the point I said above, but I do not generate any command breaking line for terminals since it differ on OSes (Mac/Linux and Windows) and terminals (Terminal, powershell, cmd, etc). I couldn't find a good solution to work with all that and it'd give many maintainability problems in the future. Then it'd the users responsability of doing that. Very similar as the default postman behavior. Do you agree or have any better idea?

Thanks for the update. I wrote some comments as part of the review.

Regarding 1, sounds good.
Regarding 2, I agree, let's keep it like that for now. Also, we don't have access to the rawBody where the logs are created.

When testing I noticed that the curl command will always use localhost, even though we can now have cloud logs originating from a xxxx.mockoon.app instance. I will merge on an intermediary branch and fix this as you don't have an easy way to test that right now.

Update: Don't investigate the failing Windows test, it must be flaky as it's passing locally and on Linux.

@Mavbraz Mavbraz force-pushed the feature/copy-log-as-curl branch from 687c63b to 1003fd8 Compare November 1, 2025 12:05
@Mavbraz
Copy link
Contributor Author

Mavbraz commented Nov 1, 2025

Is this PR going to be merged with squash commit? If not, I can do it to prevent make the main a mess.

@Mavbraz Mavbraz requested a review from 255kb November 1, 2025 14:31
@255kb
Copy link
Member

255kb commented Nov 4, 2025

Is this PR going to be merged with squash commit? If not, I can do it to prevent make the main a mess.

Yes commits will be squashed.

255kb
255kb previously approved these changes Nov 4, 2025
Copy link
Member

@255kb 255kb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the PR!
It looks good. I will see if all tests are passing and merge it. I will then add some small changes to take the cloud URLs into account, especially in the new web app where there are only cloud instance URLs.

Update: it seems tests are failing

@Mavbraz
Copy link
Contributor Author

Mavbraz commented Nov 10, 2025

Thanks for the review! I fixed the failing tests.

Although, I found an edge case of copy as curl for WebSocket. The copy as curl is enable for it and the curl command is functional when used by terminal, but it doesn't work on Postman.
I'm wondering if that's good or not to be enabled for the WebSocket command... I don't see problems to keep that, but it may confuse some people since it doesn't work on Postman.

image

@255kb
Copy link
Member

255kb commented Nov 10, 2025

Thanks for the review! I fixed the failing tests.

Thanks! I will merge and implement the Cloud logic.

Although, I found an edge case of copy as curl for WebSocket. The copy as curl is enable for it and the curl command is functional when used by terminal, but it doesn't work on Postman. I'm wondering if that's good or not to be enabled for the WebSocket command... I don't see problems to keep that, but it may confuse some people since it doesn't work on Postman.

If it's working with curl it's ok. We don't have to accomodate for each and every tool out there.

@255kb 255kb changed the base branch from main to feature/copy-as-curl November 10, 2025 08:46
@255kb 255kb merged commit 44aadb2 into mockoon:feature/copy-as-curl Nov 10, 2025
11 of 12 checks passed
255kb added a commit that referenced this pull request Nov 10, 2025
* Feature/copy log as curl (#1458) (#1683)

Closes #1458

* Add copy as curl support for cloud URLs and web app

Remove test that makes the pipeline fail on GitHub Actions only (no idea why)

---------

Co-authored-by: Mavbraz <18542083+Mavbraz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy log as cURL

4 participants