json call to MailChimp API not working - json

I'm calling the ListSubscribe part of the API with this json data:
{"apikey":"XXX-us2","id":"XXXXX" ,"email_address":"joe#gmail.com","double_optin":false,"merge_vars":{"GROUPINGS":{"name":"Lectures","groups":"Lecture"}}}
and I'm getting back:
{"error":"\"L\" is not a valid Interest Grouping id for the list: members-announ
ce","code":270}
However, when I use id rather than name I get pretty much the same response (the docs say both are usable). I can get things to work if I leave out the GROUPINGS call.
The following is the enumeration of the groupings:
result:
[{"id":11941,"name":"Lectures","form_field":"hidden","display_order":"0","groups
":[{"bit":"1","name":"Transit of Venus Wellington 12 June 2012","display_order":
"1","subscribers":1},{"bit":"2","name":"Transit of Venus Auckland 14 June 2012",
"display_order":"2","subscribers":1},{"bit":"4","name":"Lecture","display_order"
:"3","subscribers":0}]}]

just a quick test to do for you.
I faced this problem yesterday with c#. My mistake was on the groupings structure.
Have you try this?
"merge_vars":{"GROUPINGS": [{"name":"Lectures","groups":"Lecture"}]}
I haven't tested because I don't need for my project: this is a simple reasoning consequent to my c# experience.
Hope that works!

Here is a sample json encoding that I have tested for listUpdateMember and works without issue.
Same layout as #Sandrous but I thought I'd demonstrate the approach for naming multiple interests in a group, along with the a full json encoding.
{"apikey":"xxxx","id":"xxx","email_address":"xxx","merge_vars":{"GROUPINGS":[{"name":"Title","groups":"group1,group2,group3"}]}}

Related

Accessing Lists in Django request.POST

I am having difficulty accessing all the data returned by my forms in my post function. I notice a significant discrepancy between what is displayed when I print request.POST vs. when my code accesses this data. Hopefully someone can explain this to me.
Output of print(request.POST):
print(request.POST)
<QueryDict: {'csrfmiddlewaretoken': ['AXMPO...'],
'start_date': ['2019-03-01'], 'end_date': ['2019-03-26'],
'reports': ['4', '1']}>
In order to examine the data my code is dealing with I used the json module to view the data. The behavior of my code during debugging conforms to this representation:
json.dumps(request.POST)
'{"csrfmiddlewaretoken": "AXMPO...",
"start_date": "2019-03-01", "end_date": "2019-03-26",
"reports": "1"}'
It all looks pretty similar until you see the "reports" value. The user selects these reports via an MultipleSelect widget on my form and my code is iterating through the id numbers provided. However, no matter how many reports I select I only get one ID. If anyone can explain why this is happening I would sincerely appreciate it.
Turns out this is a really old school issue. I could wish this was more prominent in the documentation though. The explanation by Simon Willson is below:
"""
This is a feature, not a bug. If you want a list of values for a key, use the following:
values = request.POST.getlist('key')
The reasoning behind this is that an API method should consistently return either a string or a list, but never both. The common case in web applications is for a form key to be associated with a single value, so that's what the [] syntax does. getlist() is there for the occasions (like yours) when you intend to use a key multiple times for a single value.
""" - Simon Willson, 13 years ago.

PostMan: How can I copy an array/segment from one response to the next request?

first post on here!
I have been playing around with postman automation and managed to copy over ID strings and FAB Id's.
I have seen similar questions asked but didn't quite highlight how I was hoping to do it...
Availability Response;
<SessionInfo FABSessionId="FAB123456789"/>
<VehicleSearchResponse Success="true" ModifiedRequest="false">
<ViewInfo Offset="0" Length="30"/>
<ResultSetInfo SortCode="cost" SortAscending="true" MaxItems="900" NumItems="30"/>
<Vehicles>
<Vehicle StartDate="20181002" EndDate="20181006" PickUpPoint="XXX" DropOffPoint="XXX">
<Image URL="https://picture.net/example.jpg"/>
<MoreInfoTexts>
<MoreInfo Header="..."/>
<MoreInfo Header="..."/>
<MoreInfo Header="..."/>
</MoreInfoTexts>
<PickUpDepotDetails Telephone="012345 67890">
<Address Address1="..." CityOrTown="..." Country="..."/>
</PickUpDepotDetails>
<DropOffDepotDetails Telephone="012345 67890">
<Address Address1="..." CityOrTown="..." Country="..."/>
</DropOffDepotDetails>
</Vehicle>
........
</Vehicles>
</VehicleSearchResponse>
See above... I want to be able to copy the whole "Vehicle" segment from the availability response to the costing request... It is an array so there is 20 other results below .......
Costing Request:
<Itinerary EndDate="20181006" ItineraryId="V!XXX!12345!ABC123!XXX!ABC321!XXX" StartDate="20181002">
<Vehicles>
{{vehicleSetter}}
</Vehicles>
<OptionalExtras>
So the whole Vehicle - /Vehicle segment needs to be copied to {{vehicleSetter}}.
Is there a way to do this using similar (Costing) Pre-RQ scripts to;
pm.globals.get("fabSessionSetter");
pm.globals.get("vehicleSetter");
and maybe something in (Avail.) Tests script like
pm.globals.set("vehicleSetter", jsonObject.FAB_VehicleAvailRS.VehicleSearchResponse.Vehicles.Vehicle[2]);
Let me know if you need any more information... Thanks!
First of all, I would ask devs to use json type data for passing to instead of XML. it's much more convenient, I went this way on the begging of the development of a new project, it brought me many benefits. But in case it's possible in your case.
it's not necessary to use global variables.Environment variables are enough.
But try this, I never did it, but perhaps it will help you.
https://github.com/cheeriojs/cheerio
https://www.w3schools.com/xml/xpath_syntax.asp
I think I had a similar problem to you, having to convert json into xml, either way I had a fantastic response from Sivcan Singh
Convert XML to JSON for extracting data then convert JSON back to XML in Postman

How to create RegEx with SubMatches of the same Match that capture 2 different types of output?

I'm trying to get my Jira data via JSON REST API into Excel, i.e. using VBA, and I'm parsing JSON output using RegEx. There are plenty of useful tutorials on the web, and after a couple of days I do have more or less working solution I'm happy with, except one minor obstacle. Long story short:
Among many issue fields I need friendly Assignee name, but some issues in my projects may be Unassigned, that obviously results in TWO VERY different kinds of JSON output:
Unassigned issue:
..."assignee":null,"updated"...
Assigned issue:
"assignee":{
"self":...
<Lots of NOT needed fields here>
...
},
"displayName":"Doe, John", <-- That's what I need, name only part
"active":...
<Lots of NOT needed fields here>
...
},
"updated"...
Well, I suppose that something like:
"assignee".*?"displayName":"(.*?)"|"assignee":(.*?),"updated"
will handle the job by producing TWO possible Matches, but... Is there a way to create RegEx where ANY of output options will result in SubMatches of ONE Match?
I'm a total newbie to RegEx, so sorry if the wording of my question is silly due to incorrectly used terms. Anyway, I hope the sample part is more or less clear, and I'll be extremely grateful for useful suggestions.
After an hour of tryouts on regex101 I ended up with the following RegEx:
"assignee":(null|.*?"displayName":"(.*?)","active")
Probably it's ugly and may be improved - but it DOES the job, and does NOT ruin in the process the indexes of subsequent Matches in collection, therefore keeping the rest of code working as it is now.

Passing a command line argument (as a string) into my Perl script

I'm extremely new at Perl and trying to prove I can pick it up quickly. What I was asked to do is add a string as an argument on my command line, and then feed that into my script. From there it is supposed to search a MySQL table I've made for matches in one column, and spit the contents of another column into an array. It was suggested I used the Getops:Std but I'm uncertain how exactly to do that, and if that's the best technique.
For example: I have a MySQL table with car manufacturers, and car models. I want to run, Perl myscript.pl Ford, and then have it shoot me back an array with
Mustang
Escape
Focus
But I'm uncertain how to get that string input in the first place. Would Getops:Std be best? If so how would it be written? I'm picking this up quickly, but I've been at it less than a week, so the simpler the explanation, the better.
Edit: Basically I was confused why it was suggested I should use GetOpts::Std for this. It seems to be completely inappropriate for what I'm trying to do.
GetOpts::Std is overkill for this. Your command line arguments are in #ARGV. If you haven't been able to work that out after a week, then you need better references for Perl.
The first argument will be in $ARGV[0], the second in $ARGV[1] , and so on.
You should check the DBI module. Google for some tutorial out there.
Then try to write your script and post more specific questions with some code if you need more help.

Extract all words from text field in mysql

I have a table that contains text fields. In those fields I store text. There are around 20 to 50 sentences in each field depending on the row. I am making an auto-complete HTML object with HTML and PHP, and I would like to start typing the beginning of a word and that the database return sentences containing those words (Like Microsoft office 2007/2010 navigation pane).
I need mysql to return those words or sentences as a separate result, so i can manipulate them further.
Example:
--------------------------------------------------------------------
| id | title |content |
--------------------------------------------------------------------
1 | test 1 | PHP is a very nice language and has nice features.
2 | test 2 | Spain is a nice country to visit and has nice language.
3 | test 3 | Perl isn\'t as nice a language as PHP.
I need mysql query to return following as different result:
1,"nice language"
1,"nice features"
2,"nice country"
2,"nice langugage"
3,"nicea a language"
Here is my sql query:
SELECT id, SUBSTR(content,POSITION('nice' IN content),50)
FROM entries
MATCH (title,entry) AGAINST ('nice' WITH QUERY EXPANSION)
New Answer
OP is actually asking nothing to do with php and javascript - his question concerns doing string manipulation directly within MySQL.
String manipulation isn't really the main focus of a DBMS. When dealing with "words" in a fluid text sense, there's a lot of logic required to determine where the next word boundary is, and you don't want your database doing this really. Plus, any queries written to do this will probably be incredibly difficult to read.
It depends exactly what you are doing, but it's quite likely that a DB only approach will be slower because there will be more function calls: SQL functions are pretty limited.
And for re-usability and best practice, what if you wanted to change your database in the future to say MongoDB? You'd need to re-write the whole damned awkward query.
No, my suggestion would be to pull the whole value using standard MySQL into PHP, throw it into PCRE, very simple regex, job done. It's better to show what you're actually doing in your PHP code as it's more "intention revealing".
At least 33% of a developer's work is picking the right tool for the job. PHP is the right tool in this example.
Original Answer
You have included the tags php and javascript, so I'm guessing (although your question needs more clarification on this) that you obviously want this 'autocomplete' running client-side. So as a result, you have to get your data from server-side to client-side first.
Twitter Bootstrap has something really cool called Typeahead. This uses JavaScript to perform (what I think) you require: the example on that page shows how you can type a country and it'll auto-complete it for you. It looks like this:
How do you get this working? Include the required JavaScript file first, and then write your HTML.
Here's some from the source code of the bootstrap page so you can see how it works:
<input type="text" data-provide="typeahead" data-items="4" data-source='["Alabama","Alaska","Arizona","Arkansas","California"]'>
Can you see how the data-source attribute is the one that gives the typeahead the information you want? You want to connect to MySQL, grab your data, and shove these into the data-source array for the JavaScript to work with, as above.
So, on your page load, you connect to MySQL and you pull all the relevant strings you would like to be "auto-complete-able" from the Database. You then put these as new Data attributes for the typeahead, and that's pretty much it!
--
Edit: There's a fork of twitter bootstrap's typeahead that allows AJAX calls, so you could use this to perform the data retrieval asynchronously (if you can figure it out, I'd recommend this approach).