Skip to content

Commit 70f94a6

Browse files
authored
Example with POST (#58)
* Example with POST We now have an example of receiving a POST, and state POST semantics. resolves #57
1 parent 8dc8b6d commit 70f94a6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

index.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,46 @@ <h2>
168168
shared. If the ShareData contains no information for a given member,
169169
the query parameter is omitted.
170170
</p>
171+
<p>
172+
A share target might only be interested in a subset of the
173+
<a data-cite="WebShare#dom-sharedata">ShareData</a> members. This
174+
example also shows a share target that receives data as a
175+
<code>POST</code> request, which should be the case if the request
176+
causes an immediate side effect.
177+
</p>
178+
<pre class="example json" title="manifest.webmanifest">
179+
{
180+
"name": "Bookmark",
181+
"share_target": {
182+
"action": "/bookmark",
183+
"method": "POST",
184+
"enctype": "multipart/form-data",
185+
"params": {
186+
"url": "link"
187+
}
188+
}
189+
}
190+
</pre>
191+
<p>
192+
The shared information might be read by a <a data-cite=
193+
"service-workers-1#service-worker-concept">service worker</a>, rather
194+
than being sent over the network to the server.
195+
</p>
196+
<pre class="example javascript" title="sw.js">
197+
self.addEventListener('fetch', event =&gt; {
198+
if (event.request.method !== 'POST') {
199+
event.respondWith(fetch(event.request));
200+
return;
201+
}
202+
203+
event.respondWith((async () =&gt; {
204+
const formData = await event.request.formData();
205+
const link = formData.get('link') || '';
206+
saveBookmark(link);
207+
return new Response('Bookmark saved: ' + link);
208+
})());
209+
});
210+
</pre>
171211
<p>
172212
How the handler deals with the shared data is at the handler's
173213
discretion, and will generally depend on the type of app. Here are some
@@ -325,6 +365,12 @@ <h3>
325365
"https://tools.ietf.org/html/rfc7231#section-4">method</a> for the
326366
<a data-link-for="web share targets">web share target</a>.
327367
</p>
368+
<p class="note">
369+
A use case for <code>GET</code> requests is when the share target
370+
drafts a message for subsequent user approval. If the share target
371+
performs a side-effect without any user interaction,
372+
<code>POST</code> requests should be used.
373+
</p>
328374
<p>
329375
The <dfn>enctype</dfn> member specifies how the share data is encoded
330376
in the body of a <code>POST</code> request. It is ignored when

0 commit comments

Comments
 (0)