wrkbrs

Application/x-www-form-urlencode 및 content-type 본문

Web dev

Application/x-www-form-urlencode 및 content-type

zcarc 2019. 2. 18. 16:09
15

I've googled for a while now and can only find what processData: false does. I can't find anyone who has experienced this same issue.

I'm passing JSON back to the server and do not want jQuery to automatically convert the data to a query string so I'm setting processData to false. I can see the request firing if I take out processData, but as soon as I put it in I do not see any requests being made (using Firebug & Chrome dev tools).

$.ajax({
            url: myUrl,
            type: "POST",
            data: {foo: "bar"},
            processData: false,
            contentType: 'application/json'
        });

The request I was initially making was a bit more complex than this but I've simplified it to try to narrow down the problem but this simple piece of code does not work either (again, it does work if I comment out processData). Also, I am not seeing any JavaScript errors in the console.

Edit

For future web searchers: As lonesomeday pointed out, jQuery will not throw any errors if you supply either a JS object or an incorrectly formatted JSON string. It will simply not fire the request.

16

You want to pass the data as JSON. You are passing a Javascript object. JSON is a way of serializing Javascript objects to strings so that they can be passed around without compatibility issues.

You actually want to pass the JSON in a string:

$.ajax({
    url: myUrl,
    type: "POST",
    data: '{"foo": "bar"}',
    processData: false,
    contentType: 'application/json'
});
  • Thanks a lot! I've realized that the more complex data I was originally sending was also formatted incorrectly. It's interesting that jQuery does not throw any errors when trying to parse the bad JSON string. – paz Jun 6 '11 at 14:34
  • 3
    Has been some time since this post but I would like to add to @lonesomday if you want to avoid put '' by hand on each property/value you can do JSON.stringify(<a jason object>) and that's all. – vsm Nov 27 '11 at 3:04
  • 6
    But still, what does processData do? – getsetbro Sep 18 '14 at 18:26
  • 1
    @getsetbro By default, that is processData:true, jQuery automatically converts any object passed to `data' to a query string that is appended to the URL when firing the request. Setting it to false, prevents that and therefore allows you to send JSON or other data that you don't want processed. – paz Feb 13 '17 at 15:17
  • 1
    processData does nothing here. When you pass a string to data (as in this example) jQuery won't process it. – Quentin Oct 19 '17 at 20:17
7

Actually, processData by default assumes that data passed is an object and sends it as application/x-www-form-urlencoded.

Summing up everything said above by @lonesomeday and @vsm to send raw JSON (what is different from the form data) you need to:

$.ajax({
    url: 'http://here-i.am/send-me/an/angel', // Determining far end
    data: JSON.stringify({foo: "bar"}), // Obtaining proper JSON string from data object
    processData: false, // Preventing default data parse behavior
    contentType: "application/json" // Setting proper `ContentType` for our data
    ...
});



참고: https://stackoverflow.com/questions/6253256/setting-processdata-to-false-in-jquery-breaks-my-ajax-request










SubjectContent-Type 분석


Content-Type은 Internt Media 타입이다. 그러나 왜 알아야 하는 가는 웹 서버 로그의 Content-Type을 보고 어떤 어플리케이션이 사용되어 있는지를 파악할 때 도움이 된다. 또한 웹 포렌식 작업을 할 때 실제 공격차단의 룰셋인지 아니면 정상적인 서비스인지의 룰인지를 판단해야 할 때 많은 도움이 될 것이다. 최근에는 웹 이외의 어플리케이션들이 HTTP+XML 형태로 사용을 많이 하기 때문에 오탐여부를 판단할 때 많은 도움이 된다.

1) Multipart Related MIME 타입
- Content-Type : Multipart/related(기본형태)
- Content-Type : Application/X-FixedRecord
- Content-Type: Text/x-Okie; charset=iso-8859-1;

2) XML Media의 타입
- Content-Type : text/xml
- Content-Type : Application/xml
- Content-Type : Application/xml-external-parsed-entity
- Content-Type : Application/xml-dtd
- Content-Type : Application/mathtml+xml
- Content-Type : Application/xslt+xml

3) Application의 타입 
- Content-Type : Application/EDI-X12: Defined in RFC 1767 
- Content-Type : Application/EDIFACT: Defined in RFC 1767 
- Content-Type : Application/javascript: Defined in RFC 4329 
- Content-Type : Application/octet-stream: <-- 디폴트 미디어 타입은 운영체제 종종 실행파일, 다운로드를 의미
- Content-Type : Application/ogg: Defined in RFC 3534 
- Content-Type : Application/x-shockwave-flash: Adobe Flash files
- Content-Type : Application/json: JavaScript Object Notation JSON; Defined in RFC 4627 
- Content-Type : Application/x-www-form-urlencode <-- HTML Form 형태 


* x-www-form-urlencode와 multipart/form-data은 둘다 폼 형태이지만 x-www-form-urlencode은 대용량 바이너리 테이터를 전송하기에 비능률적이기 때문에 대부분 첨부파일은 multipart/form-data를 사용하게 된다. 

4) 오디오 타입
- Content-Type : Type audio: Audio 
- Content-Type : audio/mpeg: MP3 or other MPEG audio
- Content-Type : audio/x-ms-wma: Windows Media Audio;
- Content-Type : audio/vnd.rn-realaudio: RealAudio; 등등 


5) Multipart 타입(아카이브 또는 개체) 
- Content-Type : multipart/mixed: MIME E-mail; 
- Content-Type : multipart/alternative: MIME E-mail;
- Content-Type : multipart/related: MIME E-mail; Defined in RFC 2387 and used by MHTML(HTML mail) 
- Content-Type : multipart/formed-data : <-- 파일 첨부

6) TEXT 타입 
- Content-Type : text/css: 
- Content-Type : text/html:
- Content-Type : text/javascript 
- Content-Type : text/plain: 
- Content-Type : text/xml: 


7) 기타 MIMERPC 예제들 
가) HTTP with x/www-form-urlencoded 일반요청 
POST /some/resource HTTP/1.1
Content-type: application/x-www-form-urlencoded
0=example.getStateName&1=10023


[응답]
HTTP/1.1 200 OK
Content-type: text/plain
New York


나) HTTP x/www-form-urlencoded namedArgs getTeam
POST /some/resource HTTP/1.1
Content-type: application/x-www-form-urlencoded
0=example.getTeam&state=New York&sport=Baseball


[응답]
HTTP/1.1 200 OK
Content-type: multipart/mixed, boundary=B
--BYankees

--BMets

--B


다) HTTP x/www-form-urlencoded unicode addUser
POST /some/resource HTTP/1.1
Content-type: application/x-www-form-urlencoded
0=example.addUser&fname=Igna%ACio&lname=Sanchez


라) HTTP with multipart/form-data 요청
POST /some/resource HTTP/1.1
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-data; name="field1"


Joe Blow


--AaB03x 
content-disposition: form-data; name="pics"; filename="file1.gif"
Content-type: image/gif
Content-Transfer-Encoding: binary
...contents of file1.gif... 


--AaB03x--

[응답] 
HTTP/1.0 200 OK
Content-type: text/plain
OK


마) Uploading multiple files with unicode

POST /foo HTTP/1.0
Content-type: multipart/form-data, boundary=AaB03x


--AaB03x
content-disposition: form-data; name="field1"
Joe Blow


--AaB03x <-- 여러개의 파일을 첨부할 때 
content-disposition: form-data; name="pics"
Content-type: multipart/mixed, boundary=BbC04y

--BbC04y <-- 첫번째 첨부파일은 텍스트
Content-disposition: attachment; filename="file1.txt"
Content-Type: text/plain; charset=UNICODE-1-1
Content-Transfer-Encoding: binary
... contents of some unicode file.txt ...


--BbC04y <-- 두번째 첨부파일은 이미지
Content-disposition: attachment; filename="file2.gif"
Content-type: image/gifContent-Transfer-Encoding: binary
...contents of file2.gif...

--BbC04y


----AaB03x--


바) XML and EMail 요청
HTP Request 
POST /x/foo/bar HTTP/1.0
reply-to-url: callback@domain.com
message-id: abc123
aynch: required0=getAuthorization&1="bobjones"


[응답] 
HTTP/1.0 200 OK
delivered-to: callback@domain.com
Content-length: 0
Mail/SMTP Response 
To: callback@domain.comFrom: mimeRPC@otherplace.com
in-reply-to: abc123
content-type: text/xml
<?xml version="1.0"?><this><is /><xml /></this>

또한 간혹 Content-Type외에 x-vermeer-content-type 형태가 추가로 나오는 경우가 있는데 이것은 RPC 통신을 사용하는 것을 의미하며, 마이크로소프트 제품중에 SharePoint 서버 제품군과 프론트페이지가 이 x-vermeer-Content-Type를 사용하기 때문에 기억해 두기 바란다.



참고: http://www.kaav.net/board/detail.php?table=Linux&Num=14