4

I have created a javascript library and I am setting manually the Content-Type right now. I would love to know what is the default Content-Type. The XHR documentation for the send() method is confusing at best:

4. If body is null, go to the next step. Otherwise ...

BodyInit

If body is a string, set encoding to UTF-8.

Set request body and Content-Type to the result of extracting body.

Where, the result of extracting body for a text is:

  • Blob
  • BufferSource
  • FormData
  • URLSearchParams
  • USVString

So, which of these types would it be? The body passed to send() is in the default, escaped format of a=b&c=d. Is it a URLSearchParams? or a USVString? or a Blob? There's a description on each one but it's too cryptic for me to understand (I only know that it's probably not a FormData).

Note: please provide some official documentation if possible

Knu
  • 14,806
  • 5
  • 56
  • 89
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
  • @adeneo thanks for the comment, but why? do you know any reference? Is that by the specificaction, cross-browser, or just browser-specific? – Francisco Presencia Jan 22 '16 at 17:13
  • I think it isn't sent. If you check the examples [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests), there's no `Content-Type` being sent. – MinusFour Jan 22 '16 at 17:15
  • According to the spec, it seems it checks the charset, and if `null` sets it to `UTF8`, and then checks "final MIME type" and sets that, usually that would be `text/html`, meaning the `Content-type` is set to something like `text/html; charset=utf-8.` – adeneo Jan 22 '16 at 17:15
  • @MinusFour — The example you link to sets a content-type. – Quentin Jan 22 '16 at 17:16
  • @Quentin, yeah I had a bad link it's fixed. – MinusFour Jan 22 '16 at 17:16
  • @MinusFour — The new example you link to is a GET request, it doesn't have a request body to set the content-type of. – Quentin Jan 22 '16 at 17:17
  • What you linked to says that if the value is a string, the content type is set to `text/plain;charset=UTF-8`. – Felix Kling Jan 22 '16 at 17:17
  • @FelixKling — That matches what I observe if I try `var f = new XMLHttpRequest();f.open("POST", "", false);f.send("test=test");` – Quentin Jan 22 '16 at 17:18
  • @MinusFour & @Quentin yes, GET requests just ignores `Content-type` and some other headers – Francisco Presencia Jan 22 '16 at 17:19
  • @MinusFour also it says *the only headers which are allowed to be manually*. This says that the user can set them manually, but it has nothing to do apparently with the default value if not set manually. – Francisco Presencia Jan 22 '16 at 17:21
  • @FelixKling it specifies only the `USVString` string type. But `URLSearchParams`, for instance, appears to be also the desired string. That's why I'm confused – Francisco Presencia Jan 22 '16 at 17:23
  • @FranciscoPresencia, right... I only meant to show up the request example there (the actuual petitions). I just didn't realize that there wouldn't be any sense on setting a `Content-Type` for a petition that doesn't send any content. – MinusFour Jan 22 '16 at 17:23
  • 1
    @FranciscoPresencia — https://url.spec.whatwg.org/#urlsearchparams — it's a specific kind of object – Quentin Jan 22 '16 at 17:24
  • If you click on the `URLSearchParams` link (what @Quentin posted), then you will see that the `URLSearchParams` object has a specific interface which generic strings don't have. Hence a string cannot be a `URLSearchParams` object / value. – Felix Kling Jan 22 '16 at 17:32
  • Okay I see, [it's a completely different thing](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams). So it should be `USVString` then, thanks. I'll test it to check that it holds true cross-browser, thanks for all the comments/help! – Francisco Presencia Jan 22 '16 at 18:37

1 Answers1

3

Directly from the horse's mouth, if you are sending a string their last test expects "text/plain;charset=UTF-8" in accordance with the XMLHttpRequest Level 1 specification.

So you will have to refer to the step four of the send method to know exactly what is expected depending on the data.

Knu
  • 14,806
  • 5
  • 56
  • 89