Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties with arrays are not provided correctly when posting to an external blog #53

Open
paulrobertlloyd opened this issue Jul 1, 2023 · 5 comments

Comments

@paulrobertlloyd
Copy link
Contributor

When posting to an external blog, the selected categories are sent to the Micropub server using an unrecognised key name, category[] instead of category.

This is the data sent to a server by the Micro.blog app:

{
  h: 'entry',
  content: 'Test categories',
  'category[]': [ 'indiekit', 'indieweb' ],
  'mp-destination': 'https://indiekit-sandbox.netlify.app'/,
}

However, sending the same data from another app (such as Quill) will send the following data:

{
  h: 'entry',
  content: 'Test categories',
  category: [ 'indiekit', 'indieweb' ],
}

It looks like the app appends categories to a category[] field:

params.append('category[]', category)

Using URLSearchParams.append() method multiple times adds the key value pair to a URL multiple times:

let url = new URL("https://website.example");
url.searchParams.append("category", "foo")
url.searchParams.append("category", "bar")
console.log("url", url)

// https://website.example/?category=foo&category=bar

Multiple params with the same key name get serialised as an Array, so there’s no need to indicate the field is an array by adding a [] suffix. That’s my understanding, anyway.

@manton
Copy link
Contributor

manton commented Jul 4, 2023

Thanks, you're right, it should only be using the [] for form-encoded params, not for JSON.

@manton
Copy link
Contributor

manton commented Jul 4, 2023

I haven't looked at this closely yet, but I think the issue is that the Axios framework we're using sends as JSON when we really just want plain ol' form-encoded parameters here. Micropub servers can implement both but I think there's likely better compatibility not using JSON.

@paulrobertlloyd
Copy link
Contributor Author

Looks like there are some solutions to sending as form encoded params here.

Separately, if/when I’m able to test changes, I was thinking it might be nice to remove the Axios dependency and use the Fetch API instead, which is now supported by Node.

@manton
Copy link
Contributor

manton commented Jul 5, 2023

I'll defer to @vincentritter on that but I'd be fine with it. We use fetch for the XML-RPC calls and Micropub discovery already.

@vincentritter
Copy link
Collaborator

vincentritter commented Jul 5, 2023

Thanks @paulrobertlloyd and also thank you @manton.

I'm working through this at the moment. For your benefit, when I created Gluon, I had problems sending through an array of category items, as it would do the wrong thing, this is probably because of the way it's sent using FormData. Sending an array of category for example: category = [ "Category 1", "Category 2" ] will result in the following on Micro.blog:

Screenshot 2023-07-05 at 11 05 03

I need to run through more here and dig in a little, perhaps this also requires a server side change so it works as expected on Micro.blog – but the first call of action should be how the data is being sent in the first place.

You'll notice that we also do the same for other parameters like multiple photos or syndicate-to targets.

In regards of using the fetch API... yep, it's on the list and that was my plan to eventually move away from axios. Feel free to open a PR if you want to have a go 😀

Will write an update here when I have it.

@paulrobertlloyd paulrobertlloyd changed the title Categories not provided correctly when posting to an external blog Properties with arrays are not provided correctly when posting to an external blog Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants