Currently i'm trying to post an image from an vb.net website to pinterest.
I got the key and access token trough the documented way.
nut now i'm trying to send the data to the server to create a pin but i get an 500 internal server error. is there something wrong with my request that caused it or is it all on their side.
Public Function SendMessage(url As String, json As String)
Try
Dim client As New RestClient(url)
Dim request As New RestRequest(Method.POST)
request.RequestFormat = DataFormat.Json
request.AddBody(json)
dim response = client.Execute(request)
Return response
Catch ex As exception
Status = ex
Return Nothing
End Try
End Function
The used URL https://api.pinterest.com/v1/pins/?access_token={mytoken}&fields=board%2Cnote%2Cimage%2Clink
and the json message
{
"board": "{board}",
"note": "test pin2",
"link": "{hyperlink}",
"image_url": "{image url}"
}
Response
{
"message": "'str' object has no attribute 'iteritems'",
"type": "http"
}
status: "Internal Server Error"
I hope someone can point out wat i'm doing wrong.
Thanks in advance
Related
need you advice please.
I am making WebRequest to download data using API. Please tell me what I am doing wrong.
API is working as expected and was tested in Postman . co site. Below are image request parameters.
GET, Authorization, Bearer Token.
Body, Raw, Text, Text String input para.json below
postman 1
postman 2
enter Sub Test()
Dim json As String = File.ReadAllText("C:\para.json")
Dim Tokenx as string = "549539487523948576293485629384756293847"
Dim result As String
Dim request = CType(url, HttpWebRequest)
request.ContentType = "text"
request.Headers.Add(AuthorizationX)
request.Method = "POST"
Using streamWriter = New StreamWriter(request.GetRequestStream())
streamWriter.Write(json)
End Using
Dim response = CType(request.GetResponse(), HttpWebResponse)
Using streamReader = New StreamReader(response.GetResponseStream())
result = streamReader.ReadToEnd()
End Using
End Sub
enter Below are images of errors
When method is POST, Error 405: Method not allowed
When method is GET, Error: Cannot send the content body with this verb-type
para.json
[
{
"site": "1339",
"po": "55090925",
"line": 6
},
{
"site": "1339",
"po": "55090925",
"line": 10
},
{
"site": "1339",
"po": "55090926",
"line": 34
}
]
error-1
error-2
i am using the new RestSharp V107 version with net 5.0.
I am calling a custom Web API. This Web APi, when the request is not authorized responses with an httpcode 401 and this information in the body (extracted from a request made in Postman)
{
"timestamp": "2022-03-30T12:17:18.558462",
"message": "Unauthorized",
"clazz": "com.mycompany.login.service.impl.AuthenticationServiceImpl",
"method": "authenticate",
"lineno": 64,
"path": "/login"
}
With RestSharp v107 i get an exception, with the message "Request failed with status code Unauthorized", but i can't get the data (or the original 401 Unauthorized http code).
I have tried this in the code, with no luck
var optionsbase = new RestClientOptions("http://mycompany")
{
ThrowOnAnyError = true,
FailOnDeserializationError = true,
ThrowOnDeserializationError = true,
};
Is there any way to get the message in the body when 401 Status is received?
Is there any way to obtain the original message and exception code (in my code i was catching the exception, chceking the status code 401 and saving the info in the body for log)
Actually, the best way is not to force RestSharp to throw but to inspect the response instead. The RestResponse object contains the response content and the response code.
That's the code that calculates the exception:
=> httpResponse.IsSuccessStatusCode
? null
#if NETSTANDARD
: new HttpRequestException($"Request failed with status code {httpResponse.StatusCode}");
#else
: new HttpRequestException($"Request failed with status code {httpResponse.StatusCode}", null, httpResponse.StatusCode);
#endif
You can see that when you use .NET Core 3.1+ or .NET 5+, you will also get the status code in the exception, but .NET Standard doesn't support that. You still get the status code in the exception message. However, there's no way to include the response content in the exception.
I am using Marshmallow to validate incoming fields for a simple put request.
Now I am testing the error handling in the frontend to make sure I send the right error messages for the frontend.
I am usually sending data of type
{
password: string,
email: string
}
For now Marshmallow checks if the password is long enough and if the email is of format Email.
I collect all errors in a expect statement and send it to the frontend like this:
except ValidationError as err:
return make_response(
{"errors": err.messages}, status.HTTP_400_BAD_REQUEST
)
with Postman giving me e.g. this response:
{
"errors": {
"email": [
"Missing data for required field."
],
"password": [
"Missing data for required field."
],
}
}
All error messages are therefore collected within the field errors and sent back to the frontend.
When the error is sent back to the frontend I catch my error and all I get is this object:
Object {
"data": null,
"error": [Error: Request failed with status code 400],
}
How do I correctly send or receive the
errors: err.messages
field in the frontend within a make_response error response?
I found the solution to the problem I had here:
github.com/axios/axios/issues/960.
Apparently you have to access the response object or the error object that is send to axios. There is no interceptor needed. What I changed was this line, when resolving the promise to:
try {
resolved.data = await promise;
} catch (e) {
resolved.error = e.response.data;
}
before that I accessed the error with:
try {
resolved.data = await promise;
} catch (e) {
resolved.error = e;
}
The errors are stored within the response.data.
I'm a totally beginner with webrequest, so I have no idea about what cause the error I get.
I try to login on a form following the microsoft tutorial for webrequest, but when I want to get the server response, I have the following error :
"the remote server returned an error (404) not found"
So I know that the URL I use actually exist and then wonder which part of the code is bad. Maybe it's because I'm doing an HTTPS request unlike the tutorial and it changes something ?
Also, I'm a little confused by getting directly the answer from the server : shouldn't there be kind of a trigger to know when the server answered ?
Dim request = WebRequest.Create("https://ssl.vocabell.com/mytica2/login")
request.Credentials = CredentialCache.DefaultCredentials
request.Method = "POST"
Dim byteArray = Encoding.UTF8.GetBytes("_username=x&_password=x")
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim reponse = request.GetResponse() 'ERROR
MsgBox(CType(reponse, HttpWebResponse).StatusDescription)
Using ds = reponse.GetResponseStream
Dim reader = New StreamReader(ds)
MsgBox(reader.ReadToEnd)
End Using
reponse.Close()
Thank you for your time, and if you have any relevant tutorial on the topic I would be glad to read it !
The page you've mentioned does exist and uses HTTPS, but if you look at the form tag within it, it's like this:
<form class="login-form form-horizontal" action="/mytica2/login_check" method="POST">
This means it doesn't post the form back to the same URL as the page, instead it sends it to the URL contained within that "action" attribute. If you're trying to use your code to simulate the submission of the login form then it looks like you need to send your POST request to https://ssl.vocabell.com/mytica2/login_check instead.
I am trying to get a stock item quantity updated on a magento 2.1 site using REST API.
I am coding in VB.net but I get the error JSON response {"message": "Request does not match any route."}
Dim Access_Token = "XXXXXXXXXXXXX"
Try
Dim VATWebClient = New WebClient()
VATWebClient.Headers(HttpRequestHeader.Accept) = "application/json"
VATWebClient.Headers(HttpRequestHeader.ContentType) = "application/json"
VATWebClient.Headers(HttpRequestHeader.Authorization) = "Authorization Bearer " & Access_Token
Dim Response As String
Response = VATWebClient.UploadString("http://www.xxxxxx.com/rest/V1/products/xxxx/stockItems/1", "{""stockItem"":{""qty"":100}}")
Catch webEx As WebException
Dim errorMessage As String = webEx.Message
Dim errorStack As String = webEx.StackTrace
End Try
I have also tried to setup SoapUI just to test to make sure that I am calling it right and I get the same error.
I read somewhere that the webapi.xml must be updated with the API which is required I am really hoping that's not the case as the host/web developer is not very accessible!
UploadString will create a POST Request, as you can see form the API Docs, this API endpooint is PUT method only.
https://devdocs.magento.com/swagger/index_21.html#!/catalogInventoryStockRegistryV1/catalogInventoryStockRegistryV1UpdateStockItemBySkuPut
I'm not too sure how to change the method in visual basic, but I'm sure it's not too difficult.