2017-12-14 22 views
1

나는 REST 끝점을 노출하는 Azure 논리 응용 프로그램을 만들었습니다.PostAsJsonAsync가 본문 매개 변수를 게시하지 않는 것 같습니다

다음 JSON 본문은 우편 배달부를 통해 전화를 걸 때 잘 작동합니다.

 var email = new Email 
     { 
      to = "[email protected]", 
      subject = "from webapp", 
      message = "hello world" 
     }; 
     var client = new HttpClient(); 
     var uri = "maskedout"; 
     var response = await client.PostAsJsonAsync<Email>(uri, email); 

I :

{ 
    "to": "[email protected]", 
    "subject": "Hello there", 
    "message": "Hello there!!!!!" 
} 

나는이 코드와 C# PostAsJsonAsync를 사용하여 동일한 통화를 할 때, 잘 그러나

{ 
    "headers": { 
     "Cache-Control": "no-cache", 
     "Connection": "keep-alive", 
     "Accept": "*/*", 
     "Accept-Encoding": "gzip,deflate", 
     "Host": "maskedout.northcentralus.logic.azure.com:443", 
     "User-Agent": "PostmanRuntime/6.4.1", 
     "Content-Length": "99", 
     "Content-Type": "application/json" 
    }, 
    "body": { 
     "to": "[email protected]", 
     "subject": "Hello there", 
     "message": "Hello there!!!!!" 
    } 
} 

형성 들어오는 요청을 볼 수 있어요 내 REST 엔드 포인트를 성공적으로 호출 할 수 있지만 본문이 포함되어 있지 않음

수신되는 내가보기에 퀘스트 :

{ 
    "headers": { 
     "Connection": "Keep-Alive", 
     "Transfer-Encoding": "chunked", 
     "Host": "maskedout.northcentralus.logic.azure.com", 
     "x-ms-request-root-id": "9e5513c2-43961642a3688718", 
     "x-ms-request-id": "|9e5513c2-43961642a3688718.1.", 
     "Request-Id": "|9e5513c2-43961642a3688718.1.1.", 
     "Content-Type": "application/json; charset=utf-8", 
     "Content-Length": "0" 
    } 
} 

무엇이 여기에 있습니까? 우편 번호와 비교하여 C# 코드의 다른 점은 무엇입니까?

+0

당신이 제대로 직렬화지고있는 JSON을 확인하기 위해 사용하는 JSON 시리얼 라이저를 불렀다 :

나는이 문제를 얻으려면 내 자신의 확장을 만들어? 우편 발송자 요청에는 SSL을 사용하지만 코드에는 사용하지 않는 것 같습니다. 또한 chunked 인코딩은 무엇입니까. – Fran

+0

이 질문과 비슷한 형식입니다 https://stackoverflow.com/questions/35464233/how-to-disable-chunked-transfer-encoding-in-asp-net-c-sharp-using-httpclient PostAsJsonAsync 확장 메서드? – Fran

+0

청크 인코딩이 무엇인지 잘 모르겠습니다. 내 C# 코드에서 볼 수 있듯이 PostJsonAsAsync를 호출하고 내 객체를 전달하는 것 외에는 많이하지 않습니다. –

답변

0

TransferEncodingChunked 헤더 속성은 기본적으로 false이어야합니다.

그러나이 시도 : 나는 너무이 문제로 실행했습니다

 var email = new Email 
     { 
      to = "[email protected]", 
      subject = "from webapp", 
      message = "hello world" 
     }; 
     var client = new HttpClient(); 
     var uri = "maskedout"; 
     client.DefaultRequestHeaders.TransferEncodingChunked = false; 
     var response = await client.PostAsJsonAsync<Email>(uri, email); 
0

. 문제는 Content-Length 헤더 인 것으로 보입니다. 우편 배달부에서는 내용의 길이이지만 HttpClient를 사용하면 길이가 0이므로 끝 점이 비어 있다고 들으므로 본문을 무시하고있는 것 같습니다.

public static async Task<HttpResponseMessage> PostJsonAsync<T>(
     this HttpClient client, 
     string requestUri, 
     T value) 
    { 
     var data = JsonConvert.SerializeObject(value); 
     var content = new StringContent(data, 
             Encoding.UTF8, 
             MimeTypes.Json); 
     Debug.WriteLine(client.BaseAddress + requestUri); 
     return await client.PostAsync(requestUri, 
             content) 
          .WithRequestTimeout() 
          .ConfigureAwait(false); 
    }