# How to escape "&" and "+" in raw during POST client-side

**URL:** <https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705>\
**Category:** Support\
**Created:** [1 Fevereiro , 2017 09:09 UTC](https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705 "2017-02-01T09:09:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Denis\_Didkovsky](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Denis\_Didkovsky](https://meta.discourse.org/u/Denis_Didkovsky)\
**Post date:** [1 Fevereiro , 2017 09:09 UTC](https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705/1 "2017-02-01T09:09:48Z")

</div>

Appreciate your help in the following:  
we need to allow user to POST new topics client-side with some tags that include “&” (like UTF-8 symbol characters, starting with “&#” and some other)

we use client-side calls like that (prefix is like “title=New topic title&category=uncategorized&raw=”)

POST:

```plaintext
var str = prefix + raw;
         $.ajax({
            url: "http://community.yoch.tv/posts/",
            type: 'POST',
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            processData: false,
            data: str,
            success: function () {
          console.log(JSON.stringify(data));
        },
        error: function(){
            console.log("Cannot get data");
        }
        });
```

and of course, it breaks at first “&” in raw body.  
Question: is there any smart way to escape breaking at “&” tags? thanks

---

<div class="post-metadata">

**Author:** ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)\
**Post date:** [1 Fevereiro , 2017 18:31 UTC](https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705/2 "2017-02-01T18:31:43Z")

</div>

Have you tried using an object instead of a string for your data?

`data: { title: "New Topic Title", category: "uncategorized", raw: "whatever" }`

---

<div class="post-metadata">

**Author:** ![Denis\_Didkovsky](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Denis\_Didkovsky](https://meta.discourse.org/u/Denis_Didkovsky)\
**Post date:** [1 Fevereiro , 2017 19:19 UTC](https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705/3 "2017-02-01T19:19:10Z")

</div>

It doesnt work. Appreciate your advice here

```
        $.ajax({
        url: "http://community.yoch.tv/posts/",
        type: 'POST',
        dataType: 'json',
        //contentType: 'application/json', //that brings 400 mistake, doesnt look that DS accepts THAT contentType 
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        processData: false,
        data: { title: "New Topic Title with json data to check", category: "uncategorized", raw: "It might work with json POST. But it's not." },
        //that brings "Failed to load resource: the server responded with a status of 422 (Unprocessable Entity)
        success: function (data) {
      console.log(JSON.stringify(data));
    },
    error: function(){
        console.log(JSON.stringify(data));
      console.log("Cannot get data");
    }
    });

```

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [1 Fevereiro , 2017 19:26 UTC](https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705/4 "2017-02-01T19:26:55Z")

</div>

> [@Denis\_Didkovsky](#):
>
> $.ajax

Looks to me like you are trying to do a POST request without CSRF, can you confirm this JS is running on the same site that is running your Discourse?

---

<div class="post-metadata">

**Author:** ![Denis\_Didkovsky](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Denis\_Didkovsky](https://meta.discourse.org/u/Denis_Didkovsky)\
**Post date:** [1 Fevereiro , 2017 19:34 UTC](https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705/5 "2017-02-01T19:34:08Z")

</div>

Yes, this JS runs on the same DS under admin user.  
**It works with string data** (looks like contentType: ‘application/json’ is not supported) and creates new topics but cut at “&” in raw (which is understandable with urlencoded POST). that we would like to overcome.

Robin suggested json workaround, but again, it looks like contentType: ‘application/json’ is not supported for POST.

---

<div class="post-metadata">

**Author:** ![Denis\_Didkovsky](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Denis\_Didkovsky](https://meta.discourse.org/u/Denis_Didkovsky)\
**Post date:** [3 Fevereiro , 2017 06:24 UTC](https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705/7 "2017-02-03T06:24:48Z")

</div>

There is another thing to overcome - it seems that with urlencoded POST the ‘+’ character is being deleted from raw.

Appreciate if we could find solution

---

<div class="post-metadata">

**Author:** ![Denis\_Didkovsky](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Denis\_Didkovsky](https://meta.discourse.org/u/Denis_Didkovsky)\
**Post date:** [15 Maio , 2017 19:49 UTC](https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705/8 "2017-05-15T19:49:03Z")

</div>

that approach works.

```plaintext
data = { title: "New Topic Title", category: "uncategorized", raw: "whatever" };
datatopost = $.param(data);

 $.ajax({
            url: "http://community.yoch.tv/posts/",
            type: 'POST',
            dataType: 'json',
            //contentType: 'application/json',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            processData: false,
            data: datatopost,
            success: function (data) {
          console.log(JSON.stringify(data));
        },
        error: function(){
            console.log(JSON.stringify(data));
          console.log("Cannot get data");
        }
        });
```

---

<div class="post-metadata">

**Author:** ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)\
**Post date:** [8 Junho , 2024 12:44 UTC](https://meta.discourse.org/t/how-to-escape-and-in-raw-during-post-client-side/56705/9 "2024-06-08T12:44:43Z")

</div>

Este tópico foi automaticamente fechado após 2684 dias. Novas respostas não são mais permitidas.
