bpgeck
bpgeck

Reputation: 1624

Is there a benefit to using binary data for an HTTP POST request?

Say I have an HTTP POST method that simple echos the request's body and I cURL it like so:

curl -X POST -d "test non-binary data" "https://endpoint/path/to/resource"

As expected, this will result in the following callback:

"test non-binary data"

Now let's say I instead pass this data as binary like so:

curl -X POST --data-binary "test binary data" "https://endpoint/path/to/resource"

This will give me the following:

"dGVzdCBiaW5hcnkgZGF0YQ=="

But I can have my API decode the base64 data and interpret it as ASCII to return the following:

"test binary data"

As expected, this is the same as the non-binary request. So, with the results being the same, what exactly is the difference between is passing a binary vs a non-binary request body? Are there any benefits to using binary data? If so, what are some examples when I would want to use binary data instead?

Upvotes: 1

Views: 2603

Answers (2)

hakre
hakre

Reputation: 197767

As expected, this is the same as the non-binary request. So, with the results being the same, what exactly is the difference between is passing a binary vs a non-binary request body? Are there any benefits to using binary data? If so, what are some examples when I would want to use binary data instead?

Many questions at once, let's decipher this:

So, with the results being the same, what exactly is the difference between is passing a binary vs a non-binary request body?

This has been mentioned in the comments and also in answers. The difference is that when not using "binary data" parsing might occur changing the meaning of the data (as raw binary data) to something different, like a file to upload or character encoding of the shell (which might apply as well to binary data but then it being less unexpected).

Are there any benefits to using binary data?

Yes, it's more expressive. You more correctly say which data you want to transfer under any circumstances (still modifications of option argument based on your shell rule might still apply).

If so, what are some examples when I would want to use binary data instead?

I'm lazy to provide these, so just do two tests, one with and one without. As you're testing and from your question you're expecting both to be the same. If you encode that expectation in the same test-case, you can add a data-provider and then add regressions when you run over them. These changes will answer your question(s) over time. Point in case is here, that if you can't answer the questiuon and even after feedback from Stackoverflow and other resources, you need to ensure that your expectations are fulfilled, even if these are two at once.

These are your tests. Write them as you understand them. If in error, fix them later. This is what version control is for. If you are in error, your tests will tell you. Use tests for your own needs. That is basically it. You might need to do changes in future as you were wrong. However the tests normally reflect your state of mind at the time you write them. So in this case, your tests should have alreay specified that you don't understand the difference between those two options, so just assert that both do the same thing. Document that by writing your test (but don't skip one option while you think it's the same but you're not really sure it is), and you will write the right test. You can fix tests later if you become aware of a mistake and at that time the change should fully document what you expect should be done. Don't hide your questions, assert the answers wihtin the test instead. A test is easy to run again, so you can easily check what you expect.

Upvotes: 3

Stanimir Stoyanov
Stanimir Stoyanov

Reputation: 1213

According to the curl man page:

--data-binary

(HTTP) This posts data exactly as specified with no extra processing whatsoever.

If you start the data with the letter @, the rest should be a filename. Data is posted in a similar manner as -d, --data does, except that newlines and carriage returns are preserved and conversions are never done.

If this option is used several times, the ones following the first will append data as described in -d, --data.

You should not receive base64 encoded data using the --data-binary option. If you do, its not curl related.

Straight to the question - the only benefit I see is that curl will not process the data passed. If you need to preserve newlines etc it makes sense to use it.

Upvotes: 3

Related Questions