How to createsimple JSON file with Dynamics Nav

ebitasebitas Member Posts: 71
edited 2018-12-20 in NAV Three Tier
Using REST services I'm trying create a simple file to pass it Using the PUT method. example: the content should look like this ["WO_1234"]

I'm using the following function:



HttpClient := HttpClient.HttpClient();
//HttpClient.DefaultRequestHeaders.Add('x-api-operation',dhbOperation);
HttpClient.DefaultRequestHeaders.Add('x-api-key','GPES0dkIMv2jTdpo2SWyQ7vlYF03v9uc2aouKIul');
HttpClient.DefaultRequestHeaders.Add('Authorization',dhbAuthText);
HttpClient.DefaultRequestHeaders.Add('ContentType','application/json');
HttpClient.BaseAddress := Uri.Uri(BaseURL);
//MESSAGE('%1',dhbAuthText);

CASE RestMethod OF
'GET':
HttpResponseMessage := HttpClient.GetAsync(Method).Result;
'POST':
HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result;
'PUT':
HttpResponseMessage := HttpClient.PutAsync(Method,HttpContent).Result;

'DELETE':
HttpResponseMessage := HttpClient.DeleteAsync(Method).Result;
END;

HttpResponseMessage.EnsureSuccessStatusCode();
String := HttpResponseMessage.Content.ReadAsStringAsync.Result;


My question is: how to I create HttpContent to be used in the PUT method?

I see examples on the web for JasonTextWriter but it requires Propertyname Exaple: [{"Workorder":"WO_1234"}] . I just need to pass the value without property name ["WO_1234"].



Please help

Thanks

Best Answer

  • ftorneroftornero Member Posts: 524
    Answer ✓
    You could try something like this:
    StrContent := StrContent.StringContent('["WO_1234"]');
    

    Where StrContent is
    Name	DataType	Subtype	Length
    StrContent	DotNet	System.Net.Http.StringContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'	
    

    And pass this variable how HttpContent.

Answers

Sign In or Register to comment.