2012-08-06 3 views
45

문제점 : POST 요청과 함께 일부 데이터를 보내는 동안 명령 줄 도구를 사용하여 로컬 호스트 서버를 말리는 작업이 작동하지 않습니다. 오류의 원인이 될 것으로 보인다 무엇WINDOWS RESTful 서비스의 cURL POST 명령 줄

: 일부 검색 내가 알아 낸 후

curl: (6) Could not resolve host: application; No data record of requested type 
curl: (6) Could not resolve host: data goes here,; No data record of requested type 
curl: (6) Could not resolve host: data2; No data record of requested type 
curl: (3) [globbing] unmatched close brace/bracket at pos 16 

복귀 데이터의

  1. curl -i -X POST -H 'Content-Type: application/json' -d '{"data1": "data goes here", "data2": "data2 goes here"}' http:localhost/path/to/api

결과 같은 것을 상상 그 문제는 t에 사용 된 sintax가 될 수 없다. 그는 UNIX 셸에서 작동하므로 요청합니다.

Are you possibly using Windows? That so looks like a completely broken shell that doesn't properly deal with single-quotes vs double-quotes. I just tried that command line and it worked fine on my linux box. http://curl.haxx.se/mail/archive-2011-03/0066.html

나는 "\를 탈출"그 주변에 작업을 시도했지만 여전히

2.

3.

curl -i -X POST -H 'Content-Type: application/json' -d '{\"data1\": \"data goes here\", \"data2\": \"data2 goes here\"}' http: //localhost/path/to/api

curl -i -X POST -H 'Content-Type: application/json' -d '{\"data1\": \"data goes here\", \"data2\": \"data2 goes here\"}' http: //localhost/path/to/api

을 그래서 작동하지 않았다 내가 포기 했어. 윈도우 POST에 전송 된 JSON 객체와 엉망으로 보인다

+0

질문 제목에'[solved] '를 추가하지 마십시오. 질문에 대한 대답을 알아 냈다면 대답으로 게시해야합니다. –

+1

@CodyGray 경고 주셔서 감사합니다. 내 포스트를 바로 잡을거야. – Lothre1

+1

git이 컴퓨터에 설치되어있는 경우에는 curl에 git bash를 사용할 수 있습니다. 그것은 많은 두통을 저장합니다. –

답변

83
내 WIN7 x64의 노트북에 같은 문제로 실행하고 매우 유사한 명령 줄 형식을 사용하여 Win64 - Generic w SSL 레이블이 컬 버전 사용하여 작업을 얻을 수 있었다

:

C:\Projects\curl-7.23.1-win64-ssl-sspi>curl -H "Content-Type: application/json" -X POST http://localhost/someapi -d "{\"Name\":\"Test Value\"}" 

만 다릅니다 두 번째 이스케이프 버전에서는 이스케이프 된 문자와 헤더 매개 변수 값을 큰 따옴표로 묶어 사용합니다. 확실히 리눅스 쉘 구문을 더 선호합니다.

13

대체 솔루션 : 명령 행보다 사용자 친화적 솔루션 : 내가 Cygwin에서 추천 명령 줄로 유지하고자하는 사람들을 위해

If you are looking for a user friendly way to send and request data using HTTP Methods other than simple GET's probably you are looking for a chrome extention just like this one http://goo.gl/rVW22f called AVANCED REST CLIENT

:

I ended up installing cygwin with CURL which allow us to Get that Linux feeling - on Windows!

Using Cygwin command line this issues have stopped and most important, the request syntax used on 1. worked fine.

유용한 링크 :

Where i was downloading the curl for windows command line?

For more information on how to install and get curl working with cygwin just go here

전 누구에게 도움이되기를 바랍니다.

+0

이것은 허용 된 대답이 아니어야합니다. OP는 웹 기반 REST 클라이언트 및 대체 명령 줄 클라이언트에 대한 권장 사항을 묻지 않았습니다. 그들은 curl에 대한 정확한 탈출을 위해 도움을 요청했으며, 이는 사용자 1683523에 의해 답변되었습니다. –

+0

하지만 그 질문을하는 많은 사람들에게 더 좋은 대답입니다. – RickAndMSFT

35

따옴표로 싸우는 것보다 쉬운 다른 명령 줄은 파일에 json을 넣고 curl 매개 변수의 @ 접두사를 사용하는 것입니다 (예 :. json에서 다음과 같이.TXT는 :

{ "syncheader" : { 
    "servertimesync" : "20131126121749", 
    "deviceid" : "testDevice" 
    } 
} 

은 내 경우에는 내가 발행 :

curl localhost:9000/sync -H "Content-type:application/json" -X POST -d @json.txt 

너무 JSON 더 읽기 유지합니다.

+0

당신은 생명의 은인입니다! –

+0

이 하나의 일, thx! – kukudas

+0

oh sweet ronald mcdonald 감사합니다. –

0
  1. 시도가 "(') 대신에 하나의 사람.
  2. 를 JSON 형식으로 따옴표를 유지하려면를 두 배로 노력 따옴표()를 사용하는 (" ").
  3. 데이터 내부 따옴표를 유지하려면 시도 에 (\\ "")이처럼을 두 번 탈출.

    curl ... -d "{""data1"": ""data1 goes here"", ""data2"": ""data2 goes here""}" 
    curl ... -d "{""data"": ""data \\""abc\\"" goes here""}" 
    
2

이상 (현재 7.33.0에 따라 Generic Win64 no-SSL binary) 내가 테스트 윈도우 바이너리 버전에 대한 명령 줄 인수가 구문 분석되는 방식에 제한이있을 수 있습니다. xmas의 답은 배치 파일에서도 작동하는 해당 설정의 올바른 구문을 설명합니다. 제공된 예를 사용 :

curl -i -X POST -H "Content-Type: application/json" -d "{""data1"":""data goes here"",""data2"":""data2 goes here""}" http:localhost/path/to/api 

클리너 대체하는 명령 줄을 구문 분석하는 데 사용됩니다 어떤 라이브러리에 의존 탈출 문자를 처리하는 것을 방지하기 위해, 별도의 파일에서 표준 JSON 형식의 텍스트를하는 것입니다 :

curl -i -X POST -H "Content-Type: application/json" -d "@body.json" http:localhost/path/to/api 
+1

감사합니다. 그것은 중요하지 않지만 지금은 윈도우 10 또는 설치된 컬 버전과 함께, 나는 지금 내 json 파일 인자 주위에 -d 뒤에 따옴표를 넣어야한다. 그렇지 않으면 라인 엔딩이 그것을 망칠 것이다. – joezen777