Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Error 422 in PUT method / System.Net.HttpWebResponse

(0) ShareShare
ReportReport
Posted on by 1,535

I have in AX2012 method like this:

public void SendRequest()
{
CLRObject clrObj;
System.Net.HttpWebRequest webReq;
System.Net.ServicePoint servicePt;
System.Net.WebHeaderCollection headers;
System.Text.Encoding utf8Enc;
System.IO.Stream stream;
System.Byte[] bodyBytes;
System.Net.WebException ex;
System.Net.HttpWebResponse webExRes;
System.Net.HttpWebResponse webRes;
System.IO.StreamReader streamReader;
System.Net.HttpStatusCode HttpStatusCodeTmp;
System.IO.BinaryReader binaryReader;
System.Int32 countOfBytes;
System.Byte[] responseBinaryBytes;
System.IO.MemoryStream memStream;
str strAuth;
boolean debug = true;
boolean CommitRequest = true;
;
try
{
new InteropPermission(InteropKind::ClrInterop).assert();
clrObj = System.Net.WebRequest::Create(requestPath);
webReq = clrObj;
servicePt = webReq.get_ServicePoint();
servicePt.set_Expect100Continue(false);
webReq.set_Method(requestMethod);
webReq.set_KeepAlive(true);
webReq.set_AutomaticDecompression(System.Net.DecompressionMethods::GZip);
headers = new System.Net.WebHeaderCollection();
headers.Add("Auth", strAuth);
headers.Add("Timestamp", timestamp);
webReq.set_Headers(headers);
utf8enc = System.Text.Encoding::get_UTF8();
stream = webReq.GetRequestStream();
bodyBytes = utf8enc.GetBytes(requestBody);
stream.Write(bodyBytes, 0, bodyBytes.get_Length());
webReq.set_ContentType("application/json");
webReq.set_Accept("*/*");
try
{
webRes = webReq.GetResponse();

info(strfmt("I'm here after GetResponse"));
}
catch (Exception::CLRError)
{
error (strFmt("Exception::CLRError"0));
}
catch
{
error (strFmt("Otjer error"));
}
CodeAccessPermission::revertAssert();
}
catch(Exception::CLRError)
{
throw error(AifUtil::getClrErrorMessage());
}
info(strfmt("Method end"));
}


If I send POST - all is OK; (error code 201, 403) -)
If I send PUT and receive code 204 - all is OK
If I send PUT and receive code 422 (Unprocessable Entity (WebDAV) (RFC 4918)) AX stops and I don't see message "I'm here after GetResponse"

What is wrong ??? Why I can't use try..catch .. in error 422 ? AX breaks all code after webReq.GetResponse ! What can I do ?

  • Ireneusz Smaczny Profile Picture
    Ireneusz Smaczny 1,535 on at
    RE: Error 422 in PUT method / System.Net.HttpWebResponse

    Thank you, Martin!

    Yes, this code was in ttsbegin / ttscommit. After changing the code all is fine and I have proper respond in PUT method!

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,458 Most Valuable Professional on at
    RE: Error 422 in PUT method / System.Net.HttpWebResponse

    Isn't your code inside a transaction (ttsbegin / ttscommit)? You can't catch exceptions inside transactions, at least most of exception types.

  • Martin Dráb Profile Picture
    Martin Dráb 230,458 Most Valuable Professional on at
    RE: Error 422 in PUT method / System.Net.HttpWebResponse

    First of all, let me format your code to make it easier to read. Please use Insert > Insert Code (in the rich formatting view) to paste source code.

    I also removed many unused variables.

    public void sendRequest()
    {
        CLRObject clrObj;
        System.Net.HttpWebRequest webReq;
        System.Net.ServicePoint servicePt;
        System.Net.WebHeaderCollection headers;
        System.Text.Encoding utf8Enc;
        System.IO.Stream stream;
        System.Byte[] bodyBytes;
        System.Net.HttpWebResponse webRes;
        str strAuth;
        
        try
        {
            new InteropPermission(InteropKind::ClrInterop).assert();
            clrObj = System.Net.WebRequest::Create(requestPath);
            webReq = clrObj;
            servicePt = webReq.get_ServicePoint();
            servicePt.set_Expect100Continue(false);
            webReq.set_Method(requestMethod);
            webReq.set_KeepAlive(true);
            webReq.set_AutomaticDecompression(System.Net.DecompressionMethods::GZip);
            headers = new System.Net.WebHeaderCollection();
            headers.Add("Auth", strAuth);
            headers.Add("Timestamp", timestamp);
            webReq.set_Headers(headers);
            utf8enc = System.Text.Encoding::get_UTF8();
            stream = webReq.GetRequestStream();
            bodyBytes = utf8enc.GetBytes(requestBody);
            stream.Write(bodyBytes, 0, bodyBytes.get_Length());
            webReq.set_ContentType("application/json");
            webReq.set_Accept("*/*");
            
            try
            {
                webRes = webReq.GetResponse();
                info(strfmt("I'm here after GetResponse"));
            }
            catch (Exception::CLRError)
            {
                error(strFmt("Exception::CLRError"));
            }
            catch
            {
                error("Other error");
            }
        }
        catch(Exception::CLRError)
        {
            throw error(AifUtil::getClrErrorMessage());
        }
        
        info(strFmt("Method end"));
    }

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans