POST to API endpoint with fetch()
See original GitHub issueHi everyone 👋
I’m implementing Staticman on my GatsbyJS site, and it’s looking great!
The only thing is that I want my comment submission event to adapt to React (no page reload for starters), so I’m turning action="https://dev.staticman.net/..." method="POST"
into a handleSubmit
function that POSTs the event via fetch()
:
async handleSubmit(event) {
event.preventDefault() // stop the form from submitting
const request = await fetch("https://dev.staticman.net/v3/entry/github/robinmetral/eaudepoisson/master/comments", {
method: "POST",
body: event,
})
const response = await request.json()
console.log(response)
}
However I get this error as a response:
errorCode: "MISSING_PARAMS"
data: [fields]
success: false
Staticman doesn’t recognize the required fields in my event
object. I’ve also tried to turn the object into a JSON string, but I’ve had the same error.
Is the API endpoint only allowing the x-www-form-urlencoded
POST format? Has anyone been using fetch()
to POST to Staticman?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Get and Post method using Fetch API - GeeksforGeeks
The fetch() method is used to send the requests to the server without refreshing the page. It is an alternative to the XMLHttpRequest...
Read more >Fetch API (JavaScript)- How to Make GET and POST Requests
Fetch API comes with a fetch () method that allows you to fetch data from all sorts of different places and work with...
Read more >Fetch API – How to Make a GET Request and POST Request ...
Fetch API – How to Make a GET Request and POST Request in JavaScript ... Simply call fetch() with the endpoint URL as...
Read more >Using the Fetch API - MDN Web Docs
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the protocol, such as requests and responses.
Read more >Fetch API POST Sample
This sample shows how the Fetch API can make POST requests. // Enter some JavaScript here // e.g. Create new GitHub Gist. Live...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After some research I’ve found a solution. Here’s my
handleSubmit
function:This is what it does:
application/x-www-form-urlencoded
content-type or “query string”, using another SO snippetIt’s probably not the only solution, and if you see something that I could improve, please let me know below or send me a PR!
@robinmetral thanks a lot, it’s worked for me, but, what was driving me nuts was this
Step 3. Hook up your forms Forms should POST to:
https://api.staticman.net/v2/entry/{GITHUB USERNAME}/{GITHUB REPOSITORY}/{BRANCH}/{PROPERTY (optional)}
But looking at your code (apart from
dev
andv3
), at the end it seems that should post there:https://
dev.staticman.net/
v3/entry/
github/{GITHUB USERNAME}/{GITHUB REPOSITORY}/{BRANCH}/{PROPERTY (optional)}
So, in my hands, this has worked for me (with semantic-ui):
and