0

I have a form:

    <form>
        <input id = "sendLink" type = "text" placeholder = "insert a link here">
    </form>

Then i trying to send data (it's a url (string) like http://sameURL.com) :

        xmlhttp.open('POST', 'http://host:8453/page.htm', true);
        xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xmlhttp.send($link);

But on server side i receive a 'splitted' post body:

"headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    ...
    more headers here
    ...
    "User-Agent": "Mozilla/5.0"
},
"httpVersion": "1.1",
"method": "POST",
"post": {
    "http://sameURL.com": ""
},
"postRaw": "http://sameURL.com",
"url": "/page.htm"

}

Why i got

"http://sameURL.com": ""

in post body instead of

"http://sameURL.com"

? Thank you. I need request without additional colon and "". I should work only with "http://sameURL.com" string.

UPD 1

Testing purpose: $link = "https://google.com?q1=asdf?q2=ddd?q3=ppp";

In that case we got:

"post": {
        "https://google.com?q1": "asdf?q2=ddd?q3=ppp"
},
"postRaw": "https://google.com?q1=asdf?q2=ddd?q3=ppp",
A.Sharma
  • 2,771
  • 1
  • 11
  • 24
sameuser
  • 13
  • 1
  • 8
  • Can you provide the dynamic initialization of the `$link` variable? I'm guessing that you are sending JSON with only 1 key in it and no values. Thus, when you decode it in PHP, accessing `arr['http://sameURL.com']` will return an empty string. – A.Sharma Jan 23 '17 at 19:01
  • I got the same result with setting of the string manually: $link = "https://google.com";. And on server : "post": { "https://google.com": "" }, – sameuser Jan 23 '17 at 19:04
  • damned parser in commentaries adds new characters and removes same symbols like h t t p. Please look at the UPD1 section in question. – sameuser Jan 23 '17 at 19:08

2 Answers2

0

The reason is that you are not sending your data properly.

Take a look at this: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send

UDP 1:

"https://google.com?q1": "asdf?q2=ddd?q3=ppp"

You have no & symbols to send more than one key value pair. Also, read up on how data is typically sent and received in these requests.

If I were you, I would do the following:

$link = "link=https://google.com&q1=asdf&q2=ddd&q3=ppp";

I haven't tested it, but it should result in:

"post": {
    "link": "https://google.com,
    "q1"  : "asdf",
    "q2"  : "ddd",
    "q3"  : "ppp"
}

Original Question

In the case of your original url:

$link = "link=http://sameURL.com";

Which would show:

"post": {
    "link": "http://sameURL.com"
}
A.Sharma
  • 2,771
  • 1
  • 11
  • 24
0

You are sending a header saying the data is form encoded, so the server is expecting key/value pairs.

I think you should try plain instead What is the difference between form-data, x-www-form-urlencoded and raw in the Postman Chrome application?

xmlhttp.setRequestHeader('Content-type', 'text/plain');
Community
  • 1
  • 1
Jim W
  • 4,866
  • 1
  • 27
  • 43