0

I have the following fetch request made via Chrome's DevTools:

fetch("https://www.example.com", {
  "headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9,he;q=0.8",
"content-type": "application/x-www-form-urlencoded",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest"
  },
  "referrer": "",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "message=hey",
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});

Now, as you can see, we have a "body" parameter with message=hey.

I am trying to get the message to say "hi" in HTML character codes, which should be like this:

hi

I need to send those HTML character code letters exactly as is with my request, but the problem is, it has symbols that break the syntax of JSON, like "&". so, how can I fix this problem?

RunningFromShia
  • 590
  • 6
  • 20
  • Maybe you want to use [Percent Encoding](https://en.wikipedia.org/wiki/Percent-encoding)? By means of `encodeURIComponent()`? Also, this shouldn't be a problem if your method is `POST`, only when your method is `GET`. You don't need to encode like that when doing `POST`. – HoldOffHunger Oct 12 '20 at 15:12
  • Good thought but it will take a lot of time without some kind of english to percent encoding translator (that will encode my letters automatically). – RunningFromShia Oct 12 '20 at 15:14
  • `encodeURIComponent()` will do it for you. Btw, you don't need to do any encoding for POST requests, only GET requests. You should be using JSON in the body, not an HTTP-escaped sequence. – HoldOffHunger Oct 12 '20 at 15:15
  • message=hey -- ***"but the problem is, it has symbols that break the syntax of JSON, like "&"."*** That's not JSON. That's a URL, GET query-string. – HoldOffHunger Oct 12 '20 at 15:18
  • Now I have a different problem. What you told me worked and devtools seemed to send exactly those letters, but in client-side i get something weird:

    &#104

    see that amp? why does it happen?
    – RunningFromShia Oct 12 '20 at 15:38
  • @HoldOffHunger OP can use whatever content type they like. For `application/x-www-form-urlencoded` special characters (non-alphanumeric) still have to be percent encoded, with exception of the space which should be replaced by `+`. – 3limin4t0r Oct 12 '20 at 15:40
  • Ah, good point! Sorry for the confusion. In that case, I recommend doing my first suggestion: `encodeURIComponent()` on the value, (i.e., on `hey`), or `encodeURI()` on the whole thing (`message=hey`). But actually, you'll want a better version of encodeURIComponent(), I have a more detailed answer elsewhere on it: [When are you supposed to use escape instead of encodeURI / encodeURIComponent?](https://stackoverflow.com/a/62436236/2430549) – HoldOffHunger Oct 12 '20 at 15:56
  • encodeURIComponent already seemed to translate it to my html character codes, but for some reason, when my server gets that message and resends it to another client, the other client sees this:

    &#104

    see that amp? it wasn't supposed to be there, it breaks the html character code.
    – RunningFromShia Oct 12 '20 at 16:06
  • @RunningFromShia That is an "issue" on the server side, not on the client. The server produces HTML. If you where to insert `

    h

    ` into the page it would display as just `h` (`

    ` is "invisible" and adds a paragraph, `&` is an HTML encoded character and will be decoded to `&`).

    – 3limin4t0r Oct 12 '20 at 16:22
  • The problem might be fixed if change *accept* header. Which currently accepts any response (`"*/*"`, which probably default to HTML on the server). Instead check what response you get if you change this to `application/json` or another [`application/*`](https://www.iana.org/assignments/media-types/media-types.xhtml#application) based MIME type of your liking (generally speaking JSON and JavaScript go well together). – 3limin4t0r Oct 12 '20 at 16:48

0 Answers0