2022 Nov 12 9:18 AM
Hi Expers,
Our company wants to integrate with a non-SAP product. We've done the GET methods, but we're having trouble with the POST methods. We need to send json data via rest api.The example code shared with us is as follows. I want to transform this code blocks to ABAP language.
static void Upload(String url, String path_locator,
String subfolder, String file, String token)
{
string requestUrl = string.Format("{0}/RestServices/UploadFile/{1}/{2}/{3}",
url,
Base64Encode(path_locator),
Base64Encode(subfolder),
Base64Encode(System.IO.Path.GetFileName(file)),
token);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "POST";
request.ContentType = "application/octet-stream";
// txtFileName contains the name of the file to upload.
byte[] fileToSend = File.ReadAllBytes(file);
request.ContentLength = fileToSend.Length;
using (Stream requestStream = request.GetRequestStream())
{
// Send the file as body request.
requestStream.Write(fileToSend, 0, fileToSend.Length);
requestStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
Console.WriteLine("HTTP/{0} {1} {2}",
response.ProtocolVersion,
(int)response.StatusCode,
response.StatusDescription);
}
Thank you all.
2022 Nov 12 10:35 AM
Your code is just about doing an HTTP call and encoding using base64, and probably UTF-8 as it sends "octets". I see nothing very specific about JSON.
Concerning HTTP call, base64 and UTF-8, you can find lots of questions and answers in the forum. Search "CL_HTTP_CLIENT", "Base64" and "CL_ABAP_CODEPAGE".
If you have questions about JSON, you can also find many questions and answers in the forum.
2022 Nov 12 5:55 PM
Yes other parameters will send via url. But how can I send JSON Data?
2022 Nov 13 9:40 AM
Could you clarify your question about sending JSON? There could be 12400 different answers.
2022 Nov 13 3:27 PM
You need to use client->request->add_multipart( ) to transfer files via cl_client_http.
You can find code samples here:
and in answers to two questions:
https://answers.sap.com/questions/271783/calling-external-rest-api-from-abap-program.html
https://answers.sap.com/questions/471502/how-to-call-multipart-api-to-transfer-file.html