Hi Sir,
In the below coding we have retrieved data using Get Method. Then I put that response in Http Content Object.
When I'm using Post method to transfer data from Business Central to 3rd Party , there I received error 'Bad Request'.
codeunit 50137 apiconnect
{
procedure APIConnect()
var
Client: HttpClient;
Response: HttpResponseMessage;
Request: HttpRequestMessage;
Header: HttpHeaders;
Result: Text;
Content: HttpContent;
newClient: HttpClient;
newResponse: HttpResponseMessage;
newRequest: HttpRequestMessage;
newHeader: HttpHeaders;
newURL: Text;
Output: Text;
newContent: HttpContent;
begin
Header := Client.DefaultRequestHeaders;
//Header.Add('User-Agent', 'Dynamic 365 Business Central');
Header.Add('Authorization', 'Basic xxx-//');
Request.SetRequestUri('xxx - // this is BC url');
Request.Method('Get');
Client.Send(Request, Response);
if Response.IsSuccessStatusCode then
Content := Response.Content
else
Message('Response was negative %1,%2', Response.HttpStatusCode, Response.ReasonPhrase);
Content.ReadAs(Result);
Message(Result);
//post method
newRequest.Method('POST');
newRequest.SetRequestUri('xxx 3rd party url');
newRequest.GetHeaders(newHeader);
newHeader.Add('accept', 'application/json');
newHeader.Add('Authorization', 'Token xxx');
newContent.WriteFrom(Result);
newContent.GetHeaders(newHeader);
newHeader.Remove('Content-Type');
newHeader.Add('Content-Type', 'application/json');
newRequest.Content := newContent;
newClient.Send(newRequest, newResponse);
if newResponse.IsSuccessStatusCode then
newContent := newResponse.Content
else
Message('Response was negative %1,%2', newResponse.HttpStatusCode, newResponse.ReasonPhrase);
end;
}
Below the coding are specified for 3rdParty Request Body.
{
"metadata": {
"open": "true",
"archived": "false",
"tags": []
},
"mode": {
"id": 1,
"code": "FTL",
"description": "Full Truckload"
},
"equipment_type": {
"id": 1,
"name": "Dry Van",
"machine_readable": "DRY_VAN"
},
"service_level": {
"code": "STD",
"id": "19"
},
"stops": [
{
"ordinal_index": 0,
"is_pickup": "true",
"location": {
"location_name": "Pickup Company",
"address": {
"address_1": "515 Congress Ave",
"city": "Austin",
"state_province": "TX",
"postal_code": "78701",
"country": "US"
},
"location_type": {
"id": "1",
"name": "Business (with dock or forklift)"
},
"point_of_contacts": [
{
"preferences": {
"cancellation": false,
"delayed": false,
"delivered": false,
"eta_changed": false
},
"email": "nathaniel.archer@gmail.com",
"first_name": "Nathaniel",
"last_name": "Archer",
"phone_number": "5122335667"
}
]
},
"planned_date": "2020-12-24",
"planned_time_window_end": "10:00:00",
"planned_time_window_start": "08:00:00"
},
{
"ordinal_index": 1,
"is_dropoff": "true",
"location": {
"location_name": "Dropoff Company",
"address": {
"address_1": "5100 Congress Ave",
"city": "Austin",
"state_province": "TX",
"postal_code": "78701",
"country": "US"
},
"location_type": {
"id": "1",
"name": "Business (with dock or forklift)"
},
"point_of_contacts": [
{
"preferences": {
"cancellation": false,
"delayed": false,
"delivered": false,
"eta_changed": false
},
"email": "nathaniel.archer3@s.gmail.com",
"first_name": "Nathaniel",
"last_name": "Archer",
"phone_number": "5122335668"
}
]
},
"planned_date": "2020-12-25",
"planned_time_window_end": "10:00:00",
"planned_time_window_start": "08:00:00"
}
],
"line_items": [
{
"description": "laptops",
"total_packages": "10",
"package_weight": "6890",
"weight_unit": "LB"
}
]
}
Below the is a json format of Bc's Data.
{
"@odata.context": " xxxxxxx -//",
"value": [
{
"@odata.etag": "W/\"JzIwOzExOTI3NDY3NDA0MTM5MzU5Njg2MTswMDsn\"",
"systemId": "xxx -//",
"open": "FTL",
"code": "DRY VAN",
"name": "STD",
"address": "AUSTIN",
"email": "NEW@GMAIL.COM",
"phoneNumber": 97490960,
"description": "PEN",
"totalPackages": 1,
"address_1": "5100 CONGRESS AVE",
"state_province": "US",
"postal_code": "9887",
"country": "US",
"location_name": "DROPOFF COMPANY"
}
]
}