PATCH / Post with curl in Classic ASP - rest

I was thrown into a pretty old project, which is made in classic ASP. For our needs, I need to make a simple curl-request, to update some data.
I'm pretty new to ASP, so I looked for similar problems. I stumbled upon this
question here:
How can I post data using cURL in asp classic?
I tried to adapt as much as possible, but it seems like I'm missing an important thing and here I need your help:
functions.asp
public function makeCurlRequest(strMethod)
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim privateKey
privateKey = "abc def"
Dim url: url = "https://sandbox.uberall.com/api/locations/322427?private_key=" & privateKey
Dim data: data = "{""location"":{""openingHours"":[{""dayOfWeek"":1,""from1"":""07:01"",""to1"":""07:02""}]}}"
'method needs to be PATCH
With http
Call .Open(strMethod, url, False)
Call .SetRequestHeader("Content-Type", "application/json")
Call .Send(data)
End With
If Left(http.Status, 1) = 2 Then
response.write("updated")
response.end()
Else
'Output error
Call Response.Write("Server returned: " & http.Status & " " & http.StatusText)
End If
end function
In my file, I simply call makeCurlRequest("PATCH").
Now it does indeed print "updated", so I guess I'm retrieving a 200, but the fields aren't updated.
Regarding to the uberall API, they require a location-object, which should be this, what is currently in my data-variable. (Checked it via a JSON-validator).
For a better readability, I'll provide the indented code as well, maybe here is an error:
{
"location":{
"openingHours":[
{
"dayOfWeek":1,
"from1":"07:01",
"to1":"07:02"
}
]
}
}
The ID's are correct, I double-checked that already. Maybe the payload is wrong? What might be the problem? Maybe data needs to be provided otherwise instead of this approach?

Looking at the examples on Uberall Tutorials Page
It looks as though the encapsulation of the location object is not necessary, instead structure the body like
{
"openingHours":[
{
"dayOfWeek":1,
"from1":"07:01",
"to1":"07:02"
}
]
}
In the code, change the data variable to be;
Dim data: data = "{""openingHours"":[{""dayOfWeek"":1,""from1"":""07:01"",""to1"":""07:02""}]}"
Must admit I had to dig around in the documentation to find an example that showed how they expected the body of the request to be structured, which isn't great for an API. Also if the payload was wrong you should be getting back an error so you know there was a problem with the payload, something along the lines of HTTP 400 Bad Request would make sense.
It's also possibly that the API uses HTTP 200 OK for everything, in which case any errors might get missed, so while testing you could just do something like this;
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim privateKey
privateKey = "abc def"
Dim url: url = "https://sandbox.uberall.com/api/locations/322427?private_key=" & privateKey
'Purposefully passing the wrong structure to see what is returned.
Dim data: data = "{""location"":{""openingHours"":[{""dayOfWeek"":1,""from1"":""07:01"",""to1"":""07:02""}]}}"
'method needs to be PATCH
With http
Call .Open(strMethod, url, False)
Call .SetRequestHeader("Content-Type", "application/json")
Call .Send(data)
End With
'Not bothered about the status for now, just give me the response.
Call Response.Write("Server returned: " & http.Status & " " & http.StatusText)
Call Response.Write("Body: " & http.ResponseText)

Related

SoapUI 5.7.0 mockRequest.requestContent is empty for POST request

I am using SOAP UI 5.7.0 to mock a REST service and it is working fine. Only, when I want to access the body of a POST request with mockRequest.requestContent, the correct content is returned only in the first call, but from then on, it always returns the empty string.
I tried the call in OnRequestScript and in the POST requests own Dispatch script but the behavior is the same in both cases. I have the suspicion, that the Stream of the request is already read somewhere else and so does not return any more content. But I don't know what to do about it.
I wonder what is the correct way to read the content of a POST request.
Thank you
Appears to be a known issue, see posts in the community forum here and here.
this seems to be an old bug of PUT operation in REST mocks.
Unfortunately, this is a bug. There is an internal defect of SOAP-2344 for this issue. This applies for the PUT and DELETE methods for a REST mock service.
I have the same issue with a PATCH request.
Use the following trick to get the body of the PUT and DELETE requests:
mockRequest.with {
if (method.toString() == 'PUT' || method.toString() == 'DELETE') {
InputStreamReader isr = new InputStreamReader(request.getInputStream(), "UTF-8")
BufferedReader br = new BufferedReader(isr)
StringBuilder sb = new StringBuilder()
while ((s=br.readLine())!=null) {
sb.append(s)
}
def requestBody = new groovy.json.JsonSlurper().parseText(sb.toString())
log.info "requestBody: " + requestBody
}
}
I use it on the project but I don't really remember how where I got the snippet from. I had to change some parts to make it work as far as I remember. Give it a try.

CodenameOne: problems using REST PATCH call

I am trying to use the Rest.patch call in CodenameOne but I am getting nothing back. If I use a REST client like ARC I get a response code of 200 and I can see the change has been made.
The Network Monitor in the CN1 Simulator shows the request being sent but shows no reply either.
Here is the code I use in CN1, Rest.post and Rest.get work well elsewhere in the code, it is Rest.patch that I am having a problem with?
bodyString= "{\"Exported\":\"1\"," +
"\"Notes\":\"Order sent\"}";
Response<Map> jsonData = Rest.patch(URL + "Orders(%27" + orderNumber + "%27)").
jsonContent().
header("Authorization", authorisation).
header("Alias", alias).
body(bodyString).
getAsJsonMap();
int responseCode = jsonData.getResponseCode();
If I send a Rest.put or Rest.post request instead, I get a 404 error, this is from either ARC or CN1.

using REST's GET in classic asp

First of all, I looked on SO already and found a similar question here:
Calling REST web services from a classic asp page
However, this doesn't solve my problem. I can't find a way to retrieve the correct information provided by my call
function determineLocationId(identifier)
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim privateKey
privateKey = "myKey"
IdentifyUrl = "https://sandbox.uberall.com/api/locations/?identifier=" & identifier & "&private_key=" & privateKey
With http
Call .Open("GET", identifyUrl, False)
Call .Send()
End With
If Left(http.Status, 1) = 2 Then
response.write("updated")
Else
'Output error
Call Response.Write("Server returned: " & http.Status & " " & http.StatusText)
End If
response.write(identifyUrl)
response.end()
determineLocationId = identifier
end function
When I print my identifyUrl to the screen and copy & paste it into my browser, I get the correct output. A json-formatted object with the information I need.
However, if I print http.responseBody, it just yields some chinese chars like this:
佄呃偙⁅呈䱍倠䉕䥌⁃ⴢ⼯㍗⽃䐯䑔䠠䵔⁌⸴㄰吠慲獮瑩潩慮⽬䔯≎∠瑨灴⼺眯睷眮⸳牯⽧剔栯浴㑬氯潯敳搮摴㸢਍ℼⴭ䐠瑡楥慮敭›敤慦汵⹴獡⁰㉖〮‰㠰〮⸸〲㈱䠠湡⵳楗汬⁩牆楥敢杲牥ⴠ
What am I doing wrong? The link is definetly the correct one. I also tried to add:
Call .SetRequestHeader("Content-Type", "application/json")
without success
Well, as it turned out, using responseBody wasn't correct. According to the MSDN-Page for WinHttpRequest, responseBody is retrieving this:
Retrieves the response entity body as an array of unsigned bytes.
In my case, I had to switch to reponseText, which does the correct thing:
Retrieves the response entity body as text.

Claiming a Ticket in RT via REST

I'm currently working on using Javascript to make REST calls to our Request Tracker system. The Javascript hasn't been a problem so far, but the documentation for RT+REST is just a little bit sparse. The goal is to claim a ticket. I've tried sending (POST):
REST/1.0/ticket/$num/take
And I get an error about not having a required parameter 'changes'
Then, I tried this:
REST/1.0/ticket/$num/take
id: $num
action: take
And I get the same error message. The last thing I've tried was:
REST/1.0/ticket/742685/edit
id: $num
action: take
And the response was "RT/ver 200 Ok" with all of the ticket information, but it didn't actually transfer ownership.
What am I missing?
On the off chance that someone else can use this, I think most of the RT REST/v1 endpoints require that all of the "key: value" strings be wrapped under a "content" key in the request payload. I also didn't see anything about a /take endpiont, so I might try something like:
var endpoint = base + 'REST/1.0/ticket/742685/edit'
var payload = {'content' : "id: 742685\n Owner: Some New Owner"}
makePost(endpoint, payload)
Worth a shot.

Can I retrieve a file from server with GET in rest

Can I return a file with my get request? I want to return a word document to calling angularJS service through REST GET method. Not sure if it is even possible.
You're a bit light on detail, so I'm gonna be a bit light on answer.
A REST request is just... a request. The REST side of things is more the way URLs are defined that what actually happens in the request process itself, which is all still vanilla HTTP.
So, same as with any GET request, if you want to return binary data, set the headers appropriately (<cfheader>), then return the file (<cfcontent>).
So this is how I did it, luckily I got this:
http://smithcustomdev.com/2010/10/20/download-file-to-browser-using-cfscript/
All I have to do was make the method remote and listen to REST service
<cfscript>
private void function serveFile(string filePath){
var fileContent = fileRead(expandPath(filePath));
var context = getPageContext();
context.setFlushOutput(false);
var response = context.getResponse().getResponse();
response.reset();
response.setContentType("text/csv");
response.setContentLength(len(fileContent));
response.setHeader("Content-Disposition","attachment; filename=#listLast(filePath,'\')#");
var out = response.getOutputStream();
out.write(ToBinary(ToBase64(fileContent)));
out.flush();
out.close();
}
</cfscript>