WebServiceURL := 'https://...'; Request := Request.Create(WebServiceURL); Request.Method := 'POST'; Request.KeepAlive := TRUE; Request.Timeout := 30000; Request.Accept('application/json'); Request.ContentType('multipart/form-data'); postString := 'param1=123¶m2=456'; Request.ContentLength := STRLEN(postString); StreamWriter := StreamWriter.StreamWriter(Request.GetRequestStream); StreamWriter.Write(postString); StreamWriter.Close;
Answers
I believe its something like internal server error.
Why there's no SOAP ACTION in the code??
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
What do you mean my soap action
I only want to send a request with parameters to a web server
I think you are trying to call a Webservice with some end point URL from NAV which is a SOAP Service, isnt it??
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
but the purpose is not call a soap service
its about Groupon and POST /tracking_notification - if you want to have a look on their API description:
https://scm.commerceinterface.com/api-doc/v2/#tracking_notification
I had no problem to get the orders (GET) but I have problems to send data (POST)
http://geekswithblogs.net/tburger/archive/2013/09/22/dynamics-nav-2013-consuming-a-rest-web-service-using-dotnet.aspx
http://www.dynamics.is/?p=2303
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
The first link does not help me because its not about how to handle httpwebrequest object (.net)
I have already read the different articles of Gunnar about Json & NAV but sadly in the exemples he never shows the thing I need to do : Howto post parameters
In the api help they give exemple for curl :
So I need to know how is done in .net the part that adds the 3 parameters (supplier, token and ci_lineitem_ids)
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.
And now I get 401 error
Thanks for this hint, I tried with httpcontent but allways error 401
I had a look on web but certainly I do not know how exactly to use this content type.
Here's my new code :
WebServiceURL := 'https://...';
Request := Request.Create(WebServiceURL);
Request.Method := 'POST';
Request.KeepAlive := TRUE;
Request.Timeout := 30000;
Request.Accept('application/json');
Request.ContentType('application/json');
postString := 'param1=123¶m2=456';
HttpStringContent := HttpStringContent.StringContent(postString,Encoding.UTF8,'application/json');
StreamWriter := StreamWriter.StreamWriter(Request.GetRequestStream);
StreamWriter.Write(HttpStringContent);
StreamWriter.Close;
If you had a working exemple of sending a post request would be nice
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.
any comment on the code posted above and the way to prepare and add parameters would be helpfull
Parameters
Local Variables
Explanation of the parameters
BaseUrl – the first part of the url that is being called, e.g.: http://www.google.com/
Method – the resource on the web service, in other words, the second part of the url, e.g. somecoolapi/parameter?option=value
RestMethod – one of the request verbs, either GET, POST, PUT or DELETE
HttpContent – the content to be sent with the POST or PUT command
HttpResponseMessage – the response message containing the status code and the response body. This parameter is ByVar because the calling code needs to handle the specific response itself. That’s not part of the generic pattern.
You have to put your data in HttpContent because you are using POST method. This is the correct way of sending parameters via post.
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.
So I'm using postasync - but I do not know if this postrequest has been accepted by the target beause when I try to read the response I get this :
<img src="https://us.v-cdn.net/5022383/uploads/FileUpload/ec/7898492df86a257032d80e1c755866.jpg" /><img src="https://us.v-cdn.net/5022383/uploads/FileUpload/ec/7898492df86a257032d80e1c755866.jpg" />
The code I'm using :
How do I know that my request has been taken in account and that there was no error ?
I'm not sure if I'm using the right way to get the answer of the target.
Finally I should get something like this :
Response:
{
"success": true
}
HttpResponseMessage.EnsureSuccessStatusCode()
HttpResponseMessage.IsSuccessStatusCode
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.
If you take a look to the PHP example in the API documentation looks like you need to send a JSON structure with the data and your id information.
req_data = {'supplier_id':'1',
'token':'xYRPKcoakMoiRzWgKLV5TqPSdNAaZQT',
'tracking_info':json.dumps([
{"carrier": "UPS", "ci_lineitem_id": 54553918, "tracking": "123456"},
{"quantity": 1, "carrier": "UPS", "ci_lineitem_id": 54553919, "tracking": "234567"}])}
response = requests.post('https://scm.commerceinterface.com/api/v2/tracking_notification', data=req_data).json()
Regards.
After correcting Code I get this code :
I tried to make everything like in indicated in your previous message but there was one thing I did not found : What should be the type of APIResult ? As I don't know the answer I took text but so I can't get APIResult.Status
Sadly, an error is thrown on HttpResponseMessage.EnsureSuccessStatusCode(); as shown in picture joint
The problem is Authorization on Groupon site (401)
As you see in commented code I tried to send supplier_id & token first in method and then in poststring
Both are not working.
//HttpResponseMessage.EnsureSuccessStatusCode();
and then check whether it is successful or not.
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.
And the content of result in the second picture
I am sure of supplier_id and token as I get the orders from Groupon without any problem
APIResult := JsonConvert.DeserializeObject(result,GETDOTNETTYPE(APIResult));
There is some issue in Deserializing the object.
Second error is the supplier_id or token is missing.
I guess u are missing the token because in case of POST web api we validate the user and we have the authentication_token i guess you are missing that.
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.
For the second error : sorry but I don't understand - can you suggest a solution ?
With little hope I just tried to use credentials with httpclienthandler, and it does not work.
Can't really see what is missing as I'm absolutely sure that supplierid and token have correct values as I watched them in debugger and compared them.
The problem is that that is not the Method
Method := STRSUBSTNO('api/v4/mark_exported?supplier_id=%1&token=%2',SupplierID,Token);
doesn't work with "mark_exported" it works with "get_orders", check de documentation.
With "mark_exported" the Method must be:
Method := 'api/v4/mark_exported';
And the suplier_id, token and real data must be send in as a JSON structure.
Regards.
But then my problem is to understand what exactly I need to send to communicate supplier_id, token and real data.
You say its in a Json structure.
In documentation for php they say : I really am not sure about how to "translate" this in Json - perhaps something like this ?
{"supplier_id": "1234", "token": "abcd", "ci_lineitem_ids": [123, 456]}
{"supplier_id": "1234", "token": "abcd", "ci_lineitem_ids": ["123", "456"]} // edited
This is what I just tried - and always the same 401 error // edited
As any caracter counts, would it be possible to help me concerning usage of {, [, " and the whole Json structure ?
But looks like something how you are trying.
Sorry I can help you more but I don't have the credentials to do any test with this API.
Regards
Having not any idea whats not working, I just wanted to check if the request is sending postdatas correctly
So I setup a testpage on my domain in which I write $_post data to a database table
And as result I was surprised :
The call with dotnet HTTPClient is not producing any postdata
So I don't know what HTTPClient is doing but perhaps I'm missing a parameter ?
Edit : I know that HTTClient is reaching my testpage because I write kind of IwasHERE in database when acceeding, just before $_post test
Zaid Tariq
Dynamics NAV/365 BC Developer at Dynamics 360
please like / agree / verify my answer, if was helpful.
And the result is clear - as soon as I specify supplier_id & token, I get a new result (missing parameters) and not 401
So this confirms what I said in my last post - the target does not receive any postdata using HTTPclient as I use it
It was impossible for me to find any kind of Json formatted text which would allow me not to see the 401 error.
But I tried x-www-form-urlencoded encoding and this was working on Postman and in my code too.
Thanks to all of you.
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.