Post Data To Web Api Like JavaScript RRS feed

  • Question

  • User-19694853 posted

    i have code in javascript  like this

     $('#submit').on('click', function (e) {
                            var rs = get_image();
                            var  _images = rs[0];
                            var   desc_ = rs[1];
                            type: "POST",
                            data: {
                                msisdn: "98780",
                                customertype: "1",
                                blob_: _images ,
                                desc_: desc_ ,
                                register_type: 1,
                                otp: "0",
                            url: "mydomain.com/image-register",

    in C# i write like this

     var data = new  
                                msisdn = "87878",
                                customertype = "1",
                                blob_ = blob_images1,
                                desc_ = desc_images1,
                                register_type = "1",
                                otp = "",
                            var content_message = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
                            client.DefaultRequestHeaders.Add("Host", "mydomain.com");
                            client.DefaultRequestHeaders.Add("Referer", "https://mydomain.com/upload");
                            client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
                            client.DefaultRequestHeaders.Add("Accept-Language", "en-GB,en;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5");
                            client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
                            client.DefaultRequestHeaders.Add("Accept", "*/*"); 
                            client.DefaultRequestHeaders.Add("Access-Control-Allow-Origin", "*");
                            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
                            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                            client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
                            var _resultUpHinh = client.PostAsync("https://mydomain.com/image-register", content_message).Result;

    List<ModelImage>  _image = new List<ModelImage>();
    List<ModelImageDesc>  _desc = new List<ModelImageDesc>();
    string[] filePaths = Directory.GetFiles(@"D:\temp\", "*.jpg");
    for (int i = 0; i < filePaths.Length; i++)
    var _modelImage = new ModelImage();
    var _modelImageDesc = new ModelImageDesc();
    string _retBase64String = ConvertToString();
    _modelImage.blob_images = _retBase64String;
    _modelImageDesc.desc_images = "";

    when i run C# console this Error like this

    {StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    Connection: keep-alive
    Vary: Accept-Encoding
    X-Frame-Options: SAMEORIGIN
    Date: Sun, 16 Sep 2018 09:00:27 GMT
    Set-Cookie: sessionid=ayba5n75g14i0rkge3y7u40ee5ytnwm0; expires=Sun, 16-Sep-2018 09:30:27 GMT; httponly; Max-Age=1800; Path=/
    Set-Cookie: TS0140a414=014743ac89c84ce1796f58ec5057d0ead455f0860df86fdc2767c5fa384f5707ea1d13c84e0a9cad77e6c00d1b6c8f31a78d781bd80c92fc866d422920d5ca16d8b8ff9788d09a5b1b737ed3fa0e125f23602997bb; Path=/
    Transfer-Encoding: chunked
    Content-Type: text/html

    how to slove that, thanks

    Sunday, September 16, 2018 9:03 AM

All replies

  • User36583972 posted

    Hi zjm_zjm,

    Usually, the HTTP 403 is returned when the client is not permitted access to the resource for some reason besides authentication.

    Firstly, please make sure the "https://mydomain.com/image-register" is correct and you can use some test tools(like postman) to get the response.

    Then, you can try to change the following code

       var content_message = new StringContent(data.ToString(), Encoding.UTF8, "application/json");


                    var content_message = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
                    var _resultUpHinh = client.PostAsync("https://mydomain.com/image-register", content_message).Result;
                    string s = await _resultUpHinh.Content.ReadAsStringAsync();


    Finally, if the error still exists and your Web API is not the ASP.NET Web API project, please contact the developer and debug.

    Best Regards,

    Yong Lu

    Monday, September 17, 2018 7:20 AM
  • User-474980206 posted

    if data you pass to $.ajax is a map used to create a url encoded form post. but your C# code is doing a json post, which may not be supported. also data.ToString(),  does not build a json string, only string with the class name.


    Wednesday, September 26, 2018 3:54 PM