web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

HTTPWebrequest

(1) ShareShare
ReportReport
Posted on by

Hi all,

I need to send a csv file to a https site.  The instructions I have been given to talk to the https have cURL commands so I am trying to duplicate this.  I'm am trying to via httpwebrequest and httpWebReponse but I can't get it to work.  I have based it on the ftpwebrequest / response from Axaptapedia but the actual send of the binary data has caused an error. 

the cURL instuctions are:- 

curl --request POST \

--header 'Authorization: bearer tokenforauthorisation' \

--header 'Content-type: text/csv' \

--header 'Accept: text/plain' \

--data-binary @/myhome/salesdata-20160512.csv \

https://a_url_to_post_to/api

to emulate this I have used the following code (currently as a job) but I get an error 415 "unsupported Media Type" when I try to get the response - debugger error appears on line  = webreq.GetResponse();

Any pointers would be greatfully accepted as I'm running out of time to get this live.

Thanks

Paulina

private void SendToAPI_PT()
{
    System.Net.HttpWebRequest               webReq;
    System.Net.HttpWebResponse              webRes;

    System.Exception    ex,webexception;
  
    CLRObject                               clrObj,httpresponse;
    System.IO.Stream                        stream;
    InteropPermission                       permission;
    System.IO.StreamReader                  streamRead;
    System.IO.StreamWriter                  streamWrite;
    System.Net.ServicePoint                 servicePt;
    System.Text.Encoding                    utf8;
    System.String                           filecontents;
    System.IO.StreamReader                  reader;
    System.Byte[]                           bytes;

    FilePath                                file;
  
    System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
   

    System.Net.Authorization   bearer      = new System.Net.Authorization('Log_access_code');
    file =                          @"C:\users\paulina\salesdata_blank.csv";
    // Read file
    reader = new System.IO.StreamReader(file);   
    utf8 = System.Text.Encoding::get_UTF8();
    bytes = utf8.GetBytes( reader.ReadToEnd() );
    reader.Close();

        //This line gives the user control to run unmanaged and managed code
    new InteropPermission(InteropKind::ClrInterop).assert();
        // Create Object for adding headers
    headers = new System.Net.WebHeaderCollection();

    clrObj = System.Net.WebRequest::Create('https://a_url_to_post_to/api');
    webReq = clrObj;
        //Set Method Type
    webReq.set_Method('POST');
    webReq.set_KeepAlive(true);

        // Set Content type
    webReq.set_ContentType('text/csv');
    webReq.set_Accept('text/plain');
    webReq.set_ContentLength(bytes.get_Length());
  
        // Add Authorization code

    headers.Add('Authorization: bearer tokenforauthorisation');


            //Add header to request
    webReq.set_Headers(headers);
        //Get Service Point
    servicePt = webReq.get_ServicePoint();
    servicePt.set_Expect100Continue(false);
        //Gets the response object

    try
    {  
       // calling [Begin]GetRequestStream before [Begin]GetResponse.

        Stream = webreq.GetRequestStream();
        Stream.Write(bytes,0,bytes.get_Length());
        Stream.Close();
  
       
        webres = webreq.GetResponse();
        info(webres.get_StatusDescription());
        webRes.Close();
    }
    catch (Exception::CLRError)
    {
        ex = CLRInterop::getLastException();


        info(ex.ToString());
        if (ex != null)
        {
            ex = ex.get_InnerException();
            if ((ex != null) && (ex is System.Net.WebException))
            {
                webException = ex as System.Net.WebException;
                info(webexception.ToString());

            }
        }


    }

}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Solved this for anyone who is interested.    The addition of API specific headers needs to be before any other header settings otherwise the .set(headers(headers) clears any other header setting you may have already set.  eg 

          // Add Authorization code

        headers.Add('Authorization: bearer tokenforauthorisation');

        webReq.set_Headers(headers);

      //Set Method Type
        webReq.set_Method('POST');
        webReq.set_KeepAlive(true);

            // Set Content type
        webReq.set_ContentType('text/csv');
        webReq.set_Accept('text/plain');
        webReq.set_ContentLength(bytes.get_Length());

    Works fine.

    Thanks for looking 

    Paulina

  • Nils Kuttkat Profile Picture
    5 on at

    That was helpful, thank you!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans