Problems with HTTP POST request(400:bad request response)

HappyBeanHappyBean Member Posts: 3
au5fcacz4eev.png
bdvx2gc79y89.png

Hey everyone,
I would like to ask for some help with a POST web request. I am a new NAV developer, and, I want to create a method to create(POST) something, using API. API it self work fine, I've tested it in a SandBox, and I want to do the same, but from navision. I managet to GET data from API also. In the photo attached to this post, you can see my code for my POST web request. First of all I want to mention that I get a "400:Bad request" error(you can see it in image attached), second, I want to mention that my request header seems to be empty, because in the debugger,"headers" variable does not contain any values, and it is Uninitialized insted.
At this moment I am stuck, and have no why what the problem could be. I would apreciate any help, ideas or hints,
Thank you very much in advance.

Answers

  • CharlesZACharlesZA Member Posts: 10
    Hi,

    I hope you find using the WebClient a bit more friendly.

    //Name DataType Subtype Length
    //WebClient DotNet System.Net.WebClient.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    // https://msdn.microsoft.com/en-us/library/0645045y(v=vs.110).aspx

    // Just an example below.
    ResultString := WebClient.UploadString('<YourURL>','POST','company_id = 12341 user_id = qewrqer');
    Life runs on code;
  • HappyBeanHappyBean Member Posts: 3
    does webclient take in count if the the string is Json or XML? because my string should be in Json format in order to be able to post something on that API
  • CharlesZACharlesZA Member Posts: 10
    HappyBean

    I can only talk from the experiences that I've had. As a hobbie I write wrappers for Bitcoin exchanges and these mostly use JSON and OAuth. Unfortunately NAV becomes a bit of a challenge when it comes to handling Byte[] arrays and JSON serialization. The webclient I've found is the most friendly, but that is my opinion and passing parameters weather it is ruby, json, or nav is just the same as http runs on bytes and bits and stuff. I would imagine your major problem would be, how to convert the response JSON data into something usable, but that is for another discussion.

    The ideal methods would be something like C#:


    string method = "POST";

    NameValueCollection POSTVALUES = new NameValueCollection();
    POSTVALUES.Add("pair", "XBTZAR");
    POSTVALUES.Add("type", "BID");
    POSTVALUES.Add("counter_volume", "0.01"); // For a "BUY" order: amount of local currency (e.g. ZAR, MYR) to spend as a decimal string in units of the local currency e.g. "100.50".

    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Concat(Properties.Settings.Default.APIKEYID, ":", Properties.Settings.Default.APIKEYSECRET)));
    WebClient.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;

    byte[] DownloadedContent = WebClient.UploadValues(Properties.Settings.Default.POSTMARKETORDERURL, method, POSTVALUES);


    From the code above you'll see that I instead of using UploadValues. In NAV I use UploadString as this converts the byte array into something usable.
    The POSTVALUES is actually a sort of string array that is converted into something similiar to the example is used in the previous post.

    I hope this helps.

    Cheers,
    Life runs on code;
  • bilalraibilalrai Member Posts: 50
    I don't understand exactly what the problem is but I have used the code below for web services. You may use it for guidance.

    LOCAL CallRESTWebService(BaseURL : Text;RestMethod : Text;HttpContent : DotNet "System.Net.Http.HttpContent";VAR HttpResponseMessage : DotNet "System.Net.Http.HttpResponseMessage")

    HttpClient := HttpClient.HttpClient();
    HttpHeaders := HttpHeaders.AuthenticationHeaderValue('Bearer', Token);
    HttpClient.DefaultRequestHeaders.Authorization := HttpHeaders;

    CASE RestMethod OF
    'GET':
    HttpResponseMessage := HttpClient.GetAsync(BaseURL).Result;
    'POST':
    HttpResponseMessage := HttpClient.PostAsync(BaseURL,HttpContent).Result;
    'PUT':
    HttpResponseMessage := HttpClient.PutAsync(BaseURL,HttpContent).Result;
    'DELETE':
    HttpResponseMessage := HttpClient.DeleteAsync(BaseURL).Result;
    END;
  • JuhlJuhl Member Posts: 724
    Json in NAV is easy with Json.net.
    I always avoid wrappers, as they are not open code, and I have been able to solve all problems using .net c/al so far.

    Sorry for the side comment
    Follow me on my blog juhl.blog
  • krikikriki Member, Moderator Posts: 9,112
    [Topic moved from 'General Chat' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.