badgateway/react-ketting

how to get the updated response from client PUT

sazyadav opened this issue · 3 comments

I need to return the updated state data with PUT response :

const put = async (url: string, data: any) => {
const client = ClientAPI.getInstance();
const resource = client.go(url);
return await resource.put({ data });

};

but getting undefined.
could you please help me to get the updated respone from the PUT request

evert commented

Hi @sazyadav . In your case, does the server change what was stored and returned the updated body in the HTTP response?

We have an open ticket to support return-representation here: badgateway/ketting#22
It would be helpful to know if fixing that ticket solves your problem.

There is a workearound right now though, if your server returns the Content-Location header (pointing to itself), this will also work:

const put = async (url: string, data: any) => {
  const client = ClientAPI.getInstance();
  const resource = client.go(url);
  await resource.put({ data });
  const newState = await resource.get();
  return newState.data;
};

This will be served from cache without doing a HTTP request with your new data if the server returned Content-Location. If you didn't return this header, it will return whatever you sent with put() last.

Hi @evert , thanks for your reply.

with const newState = await resource.get();
return newState.data;
Not getting the latest submitted data.
But with the js Fetch I am getting:
const fetchForPut = async (url: string, payload: any): Promise => {
const requestOptions = {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
};
const response = await fetch(url, requestOptions);
return await response.json();
}

How can I acheive the same with react ketting.
and also fixinga above mentoined issue ( badgateway/ketting#22 ) would be really helpful for me.

evert commented

hi @sazyadav can you edit your question to fix the formatting? it's hard to follow right now. To have syntax highlighting work code blocks need to start with

```typescript