REST POST 405 error

I'm testing out the new %CSP.REST way of creating an api and having a problem with a POST getting a 405 error.

I get data returned when I do a GET, so I don't think it's a configuration issue or a class issue.

I have no problem with %CSP.Page when creating a REST type method. So what am I missing?

Can someone give me some advice?

Thank you.

  • 0
  • 0
  • 66
  • 6
  • 2

Answers

Hi.

rror 405 refers to method not allowed.

Probably this being invoked a method that only supports GET as a POST.

Hi,

Use this Example code

Class Rest.UsuarioSvc Extends %CSP.REST
{

XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
<Route Url="/Oi/:xpto" Method="GET" Call="Oi"/>
</Routes>
}

ClassMethod Oi(xpto As %String) As %Status
{
    Set tSC = $$$OK
    
    If 'tSC Quit tSC       
    Set tProxy = ##class(%ZEN.proxyObject).%New()
    Set tProxy.Oi = xpto _ "1234"
    
    Set %response.ContentType = "application/json"
    set %response.Status = 200
    Do tProxy.%ToJSON()
    
    Quit tSC
}

}
 

You need to add POST route:

Class Rest.UsuarioSvc Extends %CSP.REST
{

XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
<Route Url="/Oi/:xpto" Method="GET" Call="Oi"/>
<Route Url="/Oi/:xpto" Method="POST" Call="Oi"/>
</Routes>
}

ClassMethod Oi(xpto As %String) As %Status
{
    Set tSC = $$$OK
    
    If 'tSC Quit tSC       
    Set tProxy = ##class(%ZEN.proxyObject).%New()
    Set tProxy.Oi = xpto _ "1234"
    
    Set %response.ContentType = "application/json"
    set %response.Status = 200
    Do tProxy.%ToJSON()
    
    Quit tSC
}

}

Hi, it's still not clear. Do I have to define both a GET and  POST for the same Route?

Is that what I'm missing?

Do I have to define both a GET and  POST for the same Route?

Yes.

Usually they have different logic so it makes more sense. What's your use case for GET and POST handlers to be the same?

Comments

Here is what I'm trying right now just to  check if the POST code is working or making it to the method.

The Route:

<Route Url="/test1" Method="POST" Call="Test1"/>

The Method:

ClassMethod Test1() As %Status
{
Set tSC=$$$OK

"test...",!

Quit tSC
}
 

This GET works:

<Route Url="/status/:ClientID" Method="GET" Call="GetActiveStatus"/>