Send moment.js object to server but get Bad request(400) error - momentjs

When I try to send a request like this:
axios.post('/apiUrl', {
startTime: moment('2021-9-1')
})
I get the Bad Request error:
{"startTime":["The value '\"2021-08-31T16:00:00.000Z\"' is not valid."]}
I found something strange from the response error:
There are 2 unexpected quotations around the value of time, and was send to server together. it seems that the quotations caused the 400 error.
Can anyone help?

moment('2021-9-1') returns a Moment object which the format your server does not expect probably. If your server expects a string, then you should format that Moment object first.
moment('2021-9-1').format()
You can pass your format inside format(). See Format
However, you should pass your format when constructing in order to avoid wrong parsing.
moment('2021-9-1', 'YYYY-M-D').format()

Related

How to get the response object when status >= 300 using http-client call-with-input-request?

How do I get the response object from call-with-input-request when the HTTP status is >= 300?
The docs say this about call-with-input-request:
Returns three values: The result of the call to reader (or #f if there is no message body in the response), the request-uri of the last request and the response object. If the response code is not in the 200 class, it will raise a condition of type (exn http client-error), (exn http server-error) or (exn http unexpected-server-response), depending on the response code. This includes 404 not found (which is a client-error).
This means that call-with-input-request signals a non-continuable condition, which (as far as I understand) means that the function does not return, and I cannot get access to the response object that would otherwise be returned. Therefore I don't see how I can actually get access to the response object corresponding to this request.
I still want to be able to inspect the response, even if its status is in the 30x-50x range. For example, I want to be able to print the HTTP reason string, or log it for debugging later. How can I achieve this?
If you trigger the exception from the REPL, you can inspect it with the ,exn comma-command. Then you'll notice the condition has a response property which is a contains the status code, headers etc.
The docs could be improved in this regard I'm sure. Perhaps you have a suggestion where to put this? The problem is that the exact contents of the condition object depend on where the condition was thrown, so not all properties will always be available.

Firebase Realtime DB "orderBy must be a valid JSON encoded path"

I'm writing a REST query for an app but I'm suddenly experiencing an error I've never gotten before. When I try to sort responses by their timestamp I get the error:
error: "orderBy must be a valid JSON encoded path"
My URL looks like https://{db url}.firebaseio.com/users/{user id}/surveys.json?auth={auth token}
My rules are set up like this:
And database is structured like this:
If I add ?orderBy="timestamp" the error shows up.
I am using correct quotation marks in query and have data indexed by timestamp in my rules. What could be happening here? Why would this suddenly no longer work after using it for a long time?
If you are using curl then
curl 'https://{db url}.firebaseio.com/users/{user id}/surveys.json?auth={auth token}&orderBy="timestamp"'
for fetch url :
https://{db url}.firebaseio.com/users/{user id}/surveys.json?auth={auth token}&orderBy="timestamp"

Using a substring of a return value in a subsequent request

I'm attempting to construct a series of Paw calls using the variables feature. I have one situation I'm unable to solve.
At authentication into the server I'm using, I get a JSON response, with one value that looks like this:
endpoint = "https://sub.something.com/thingone/thingtwo.php?token=sometoken&id=blahblah"
The endpoint portion "https://sub.something.com/" is then used as the base for subsequent calls, where a call might be "GET https://sub.something.com/data?id=123".
I don't want to hardcode the endpoint in Paw, as the endpoint will vary based on factors I can't predict at my end.
Is there a way to do basic string processing like this either in Paw, or by calling out to a shell script and using the return value of said script as a Paw variable?
That's doable using that RegExp Match dynamic value extension. Click on that previous link and hit Install Extension.
Type "Regexp" in the field you expect this value to be used. Pick Regexp Match from the completion results:
Then enter a regexp that matches your need, https?://[^/]+/? should be good:
I've put your example string in the screenshot above to show that it works, but you can instead put a "pointer" (Response Dynamic Value) to the response you want:
In the choices, pick Response Parsed Body if you want to parse a JSON or XML from the reponse. If the string is simply in plain text in the response body, pick Response Raw Body.
Once these steps are completed, you've got a working "Pointer" + "Parser" to the response that extract the part of the string you need. You can do the same operation with another regex for the token…
Tip: these dynamic value tokens can be selected like text and copy/pasted (Cmd+C/Cmd+V) :-)

Elm - retrieving string via get request

I am trying to make a get request to retrieve a string
When I use
retrieve : Task.Task Http.Error String
retrieve = getString "http://api.endpoint.com"
everything works fine.
On the other hand if I use
retrieve : Task.Task Http.Error String
retrieve = get Json.Decode.string "http://api.endpoint.com"
the http request gets done, but the chained tasks are not executed.
My question is: what is the difference between the two approaches above? Am I doing something wrong with the second one? How to debug it?
getString returns the response of the get request as a String. get take a JSON decoder and runs that over the response of the get request. So if you provide Json.Decode.string, it will expect the response to be have a Json encoded string in it. So it expects extra double quotes in the response.
If your http request fails the best way to debug is to look at what kind of error you get. In this case you'll probably get an UnexpectedPayload because the request succeeds, but the decoder fails.

Flex- handle error thrown by decodeURIComponent component for some utf-8 encoded strings

How to I handle the erros thrown by the decodeURIComponent. By handling I mean that when the decodeURIComponent throws some error I want that the characters which caused the issue to be stripped out or replaced by some default charcters.
In my case i have an encoded string which contains the 'emojis' url encoded like this '%20%f0%9f%98%8f'. So when i try to decode it using decodericomponent it creates an error may be beacuse doesn't support unicoded character.
One way i thought of handling this is
If the decodeuricomponent throws an error then create a custom function that would first replace all emojis utf-8 represtations (http://apps.timwhitlock.info/emoji/tables/unicode) with some default characters and then deocde it.
Say for eg: replace occurence of '%F0%9F%98%8A' with ':)' and so on.
But what if there are still some cases left out. Then it would again throw an error.
Can anybody help out.
Thanks in advance.

Resources