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 :
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 ?

I have the same question (0)
  • Martin Dráb Profile Picture
    237,803 Most Valuable Professional on at

    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"));
    }

  • Verified answer
    Martin Dráb Profile Picture
    237,803 Most Valuable Professional on at

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

  • Ireneusz Smaczny Profile Picture
    1,535 on at

    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!

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans